ASP.NET Core Identity for multiple project with IdentityServer4

て烟熏妆下的殇ゞ 提交于 2020-01-10 05:49:05

问题


I have one solution with two MVC projects, which use the IdentityServer4. In one project I installed the IdentityServer4 and have full access to the database. The other project is an MVC client.

When I set the [Authorize] attribute on both project all works fine, but this role attribute [Authorize(Roles = "user")] works only in project one that has the IdentityServer4, the MVC client says:

Unable to resolve service for type   'Microsoft.AspNetCore.Identity.UserManager`1[Entities.Application.ApplicationUser]' while attempting to activate 'IdMWeb.Controllers.AccountController' (this is the project one with the IdentityServer4 installed).

My questions are, why project one does not complain when I do not set the role attribute in project 2. Also, how can project 2 get the role from the database?


回答1:


please see bottom link for asp .net core apps:

Share authentication cookies path

simple answer:

please add bottom code in startup.cs ConfigureServices method of two or more ASP.NET Core projects :(be cureful the path "C:\sampleDirectoryInServer" exists in your server)

        services.AddDataProtection()
.PersistKeysToFileSystem(new DirectoryInfo(@"C:\sampleDirectoryInServer"))
.SetApplicationName("SharedCookieApp");

        services.ConfigureApplicationCookie(options => {
            options.Cookie.Name = ".AspNet.SharedCookie";
        });

it's work for me(2 asp.net core app with same database and different ports)




回答2:


Based on the error message your posted, It may be that you did not define a client on the server side for this particular client. A IdentityServer instance will only accept requests from clients that it already knows about. You have to define the acceptable clients for it when you configure the middleware.



来源:https://stackoverflow.com/questions/48708562/asp-net-core-identity-for-multiple-project-with-identityserver4

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!