【问题标题】:How to define role Identity resource in appsettings.json in Asp.net core如何在 Asp.net 核心的 appsettings.json 中定义角色身份资源
【发布时间】:2020-11-28 11:58:16
【问题描述】:

我创建了一个具有个人帐户安全性的 asp.net 核心 Web 应用程序,并将其配置为用于外部 spa 的授权/身份验证,我在 appset5tings.json 文件中添加了以下配置

 {
 "ConnectionStrings": 
{
"DefaultConnection": 
 "Server=localhost;Database=itrSts;Trusted_Connection=True;MultipleActiveResultSets=true"
 },
"Logging": {
"LogLevel": {
  "Default": "Information",
  "Microsoft": "Warning",
  "Microsoft.Hosting.Lifetime": "Information"
}
},
 "IdentityServer": {
"Clients": {
  "itr.childcare.webapp": {
    "Profile": "SPA",
    "RedirectUri": "http://localhost:3000/signin-callback",
    "LogoutUri": "http://localhost:3000/signout-callback",
    "AccessTokenLifetime": 600,
    "Scopes": "openid profile gateway-api roles"

  },
  "Itr.Sts": {
    "Profile": "IdentityServerSPA"
  }

}
"Resources": {
  "gateway-api": {
    "Profile": "API",
    "UserClaims": [
      "role"
    ]
  }

}

} }

当请求“openid profile gateway-api”范围时它工作正常但是当还请求角色范围时我得到无效范围错误谁能帮助我并告诉我应该做什么?谢谢

【问题讨论】:

    标签: asp.net-core identityserver4 claims-based-identity


    【解决方案1】:

    缺少的是身份资源定义。它们定义了客户应该可以使用的声明(包括角色)。

    请参阅article 关于身份资源。

    如果您使用的是 IdentityServer4,版本 4.x,那么您还应该考虑定义 ApiScopes

    在代码中,IdentityResources 的定义如下所示:

    var employeeInfoScope = new IdentityResource()
    {
        Name = "employee_info",
        DisplayName = "Employee information",
        Description = "Employee information including seniority and status...",
        Emphasize = true,
        Enabled = true,
        Required = true,
        ShowInDiscoveryDocument = true,
        UserClaims = new List<string>
        {
            "employment_start",
            "seniority",
            "contractor",
            "employee",
        }
    };
    
    _identityResources = new List<IdentityResource>()
    {
        new IdentityResources.OpenId(),
        new IdentityResources.Email(),
        new IdentityResources.Profile(),
        new IdentityResources.Address(),
        employeeInfoScope
    };
    

    【讨论】:

    • 我们可以在 app setting.json 中定义 ApiResource 和客户端(定义在这里:identityserver4.readthedocs.io/en/latest/topics/clients.html),我们不能为身份资源这样做吗?
    • 从未尝试过,但根据经验,在代码中执行此操作更方便,因为这样您会更加确定它可以工作(没有愚蠢的拼写或 json 错误会干扰)。
    猜你喜欢
    • 1970-01-01
    • 2021-11-05
    • 2018-08-31
    • 2019-03-26
    • 1970-01-01
    • 1970-01-01
    • 2017-12-22
    • 2021-02-02
    • 2021-12-11
    相关资源
    最近更新 更多