【发布时间】:2019-12-31 00:15:54
【问题描述】:
我有一个包含两个 MVC 项目的解决方案,它们使用 IdentityServer4。在一个项目中,我安装了 IdentityServer4 并拥有对数据库的完全访问权限。另一个项目是 MVC 客户端。
当我在两个项目上设置[Authorize] 属性时,一切正常,但此角色属性[Authorize(Roles = "user")] 仅适用于具有 IdentityServer4 的项目之一,MVC 客户端说:
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 中设置角色属性时,为什么项目 1 没有抱怨。 还有,项目2如何从数据库中获取角色?
【问题讨论】:
-
Project 2 是如何配置的?发生该错误是因为您尝试使用 ASP.net Identity UserManager 而没有注册和配置它。据我所知,这与授权属性无关。如果您希望 Project 2 能够访问您的用户管理数据库,那么您可以注册 asp.net Identity 并像往常一样使用 UserManager 类。
-
显示项目的配置以及ID4服务器如何保护它们。还展示您如何在 ID4 中为这些 MVC 项目配置资源。
标签: asp.net-mvc asp.net-core asp.net-identity identityserver4