使用IdentityServer4的多个项目的ASP.NET Core身份 [英] ASP.NET Core Identity for multiple project with IdentityServer4

查看:303
本文介绍了使用IdentityServer4的多个项目的ASP.NET Core身份的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个解决方案,其中包含两个使用IdentityServer4的MVC项目.在一个项目中,我安装了IdentityServer4,并具有对数据库的完全访问权限.另一个项目是MVC客户端.

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.

当我在两个项目上都设置[Authorize]属性都正常时,但是此角色属性[Authorize(Roles = "user")]仅在具有IdentityServer4的项目中起作用,MVC客户端会说:

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).

我的问题是,当我未在项目2中设置角色属性时,为什么项目一不抱怨. 另外,项目2如何从数据库中获得角色?

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?

推荐答案

请参阅asp .net核心应用程序的底部链接:

please see bottom link for asp .net core apps:

共享身份验证Cookie路径

简单答案:

请在两个或多个ASP.NET Core项目的startup.cs ConfigureServices方法中添加底部代码:(请确保服务器中存在路径"C:\ sampleDirectoryInServer")

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";
        });

对我有用(两个具有相同数据库和不同端口的asp.net核心应用程序)

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

这篇关于使用IdentityServer4的多个项目的ASP.NET Core身份的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆