问题
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