【问题标题】:How to dynamically loaded claims for a client, not user, IdentityServer?如何为客户端而不是用户 IdentityServer 动态加载声明?
【发布时间】:2022-01-16 13:44:12
【问题描述】:

我想知道是否/如何在 IdentityServer4 下为客户端(而不是用户)动态加载声明。对于我的 MVC 客户端应用程序,我可以使用 IdentityServer4 的 IProfileService API 为用户动态加载声明,效果很好。但是我需要对 IProfileService API 函数似乎没有涵盖的服务器到服务器客户端应用程序(客户端凭据授予类型)执行相同的操作。这可以做到吗?如果是,现在?

【问题讨论】:

    标签: identityserver4 claims-authentication


    【解决方案1】:

    也许你可以试试这个方法:

    whit Clients loaded from code:
    
        public static IEnumerable<Client> GetClients()
        {
            return new List<Client>
            {
                new Client
                {
                    ClientId = "Application1",
                    ClientName = "Application1",
                    ....
                    AllowedScopes = { "application1.api.full_access"}
                    AccessTokenLifetime = 1800,
                    IdentityTokenLifetime = 1800,
                    Claims = new Claim[]
                    {
                        new Claim("Role", "admin"),
                        new Claim(JwtClaimTypes.Name, "JwtClaimTypes.Name"),
                        new Claim(JwtClaimTypes.Role, "JwtClaimTypes.Role")
                    }
                
                }....
        }
    
    with Clients loaded via appsettings.json:
    "Clients": [
      {
        "ClientId": "Application1",
        "ClientName": "Application1",
        "Enabled": true,
        "Claims": [
          {
            "Type": "role",
            "Value": "admin"
          },
          {
            "Type": "name",
            "Value": "myapp"
          }
        ],
        ....
      }
    ]
    

    【讨论】:

    • 谢谢,但这对我们不起作用。我们在 SQL Server 上为 IdentityServer 数据存储使用扩展的 EF 模型。对于用户,我们有一个绑定到 AspNetRoles 表的 Persmissions 表。在运行时,我们的 IDS 将权限作为声明动态加载到用户的令牌中。现在这对我们来说非常有效。我们现在只想为客户端(而不是用户)做同样的事情——将权限表绑定到客户端记录并动态加载权限作为声明。我只是不确定这是否可能。
    • 嗨,不确定这是否有帮助。 identityserver4.readthedocs.io/en/latest/quickstarts/…,有一个 Clients 和 ClientClaims 表,您可以在其中将您的权限保存为声明。
    • 权限应该是标准的,应该由不同的实体(角色、客户端等)共享。我们尽量不将权限直接添加到 ClientClaims 表中,然后将它们复制到 AspNetRoleClaims 表中,然后不断重复……那将是维护的噩梦。
    • 最后的机会。在启动类 -> ConfigureServices 中: services.AddIdentityServer() .AddApiAuthorization(options => { IdentityServer4.Models.Client client = options.Clients.Where(client => client.ClientName == "Foo")。 FirstOrDefault(); client.Claims.Add(new IdentityServer4.Models.ClientClaim("role", "admin")); });
    • 再次感谢,但很抱歉,这也不能解决我的问题。我在这里需要的是一种为客户端凭据授权类型的客户端动态加载声明的方法。预定义的声明在这里帮不了我。对于用户身份,IdentityServer4 通过 IProfileService API 支持这一点,您可以在其中创建自定义声明并将它们加载到用户的 access_token 中。但是对于没有用户的应用,似乎没有办法做到这一点。
    【解决方案2】:

    我已经通过实现解决了这个问题

    public class MyClaimService : DefaultClaimsService
    

    通过覆盖此类的 GetAccessTokenClaimsAsync 函数,我可以将自定义声明添加到令牌中。与仅适用于身份的 IProfileService 不同,此功能也适用于客户端(应用程序)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-03-27
      • 2020-08-30
      • 2018-09-14
      • 2015-06-20
      • 2021-12-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多