Retrieving client name in IdentityServer4 and ASP.NET Core

北慕城南 提交于 2021-01-29 10:12:56

问题


The dbo.Clients table in the IdentityServer database contains both ClientId and ClientName, however requesting a client credentials token doesn't include the client name in the token.

Is there a way to either retrieve client information from IdentityServer given the client id, or request that the client name be added to the token?


回答1:


You can add them dynamically using a custom token request validator :

public class ClaimClientsUpdated : ICustomTokenRequestValidator
{
    public Task ValidateAsync(CustomTokenRequestValidationContext context)
    {
        context.Result.ValidatedRequest.Client.AlwaysSendClientClaims = true;
        context.Result.ValidatedRequest.ClientClaims.Add(new Claim("name", context.Result.ValidatedRequest.Client.ClientName));

       return Task.FromResult(0);
    }
}

Register in DI :

services.AddTransient<ICustomTokenRequestValidator, ClaimClientsUpdated>();

It will add prefix "client_" to custom claims , so claim will be "client_name": "value" in access token .




回答2:


You can add information about the client in general by adding claims to the ClientClaims table. E.g. Type = Name, Value = MyCustomName

Which is added as claim (assuming prefix client_):

"client_Name": "MyCustomName"

This will allow you to add information about the client without having to add or change code.

The drawback for the client name is that you'll have to add a claim with redundant information, as Clients.ClientName is not the source. The advantage is that it's configuration only.



来源:https://stackoverflow.com/questions/58386942/retrieving-client-name-in-identityserver4-and-asp-net-core

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!