簡體   English   中英

如何為客戶端而不是用戶 IdentityServer 動態加載聲明?

[英]How to dynamically loaded claims for a client, not user, IdentityServer?

我想知道是否/如何在 IdentityServer4 下為客戶端(而不是用戶)動態加載聲明。 對於我的 MVC 客戶端應用程序,我可以使用 IdentityServer4 的IProfileService API為用戶動態加載聲明,效果很好。 但是我需要對 IProfileService API 功能似乎沒有涵蓋的服務器到服務器客戶端應用程序(客戶端憑據授予類型)執行相同的操作。 這可以做到嗎? 如果是,現在?

也許您可以嘗試這種方式:

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"
      }
    ],
    ....
  }
]

我已經通過實施解決了這個問題

public class MyClaimService : DefaultClaimsService

通過覆蓋此 class 的 GetAccessTokenClaimsAsync function,我可以將自定義聲明添加到令牌中。 與僅適用於身份的 IProfileService 不同,此 function 也適用於客戶端(應用程序)。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM