【问题标题】:ASP.NET Core Identity for multiple project with IdentityServer4带有 IdentityServer4 的多个项目的 ASP.NET Core 标识
【发布时间】: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


【解决方案1】:

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

Share authentication cookies path

简单回答:

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

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

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

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

【讨论】:

    【解决方案2】:

    根据您发布的错误消息,可能是您没有在服务器端为该特定客户端定义客户端。 IdentityServer 实例只会接受来自它已经知道的客户端的请求。配置中间件时,必须为其定义可接受的客户端。

    【讨论】:

      猜你喜欢
      • 2018-12-28
      • 2016-11-26
      • 1970-01-01
      • 2023-03-24
      • 1970-01-01
      • 1970-01-01
      • 2018-02-03
      • 2020-04-16
      • 1970-01-01
      相关资源
      最近更新 更多