在IdentityServer4和ASP.NET Core中检索客户端名称 [英] Retrieving client name in IdentityServer4 and ASP.NET Core
问题描述
IdentityServer数据库中的 dbo.Clients
表包含 ClientId
和 ClientName
,但是请求客户端凭据令牌不包括令牌中的客户名称.
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.
是否有办法从给定客户端ID的IdentityServer中检索客户端信息,或请求将客户端名称添加到令牌中?
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?
推荐答案
您可以使用自定义令牌请求验证器动态添加它们:
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);
}
}
在DI中注册:
services.AddTransient<ICustomTokenRequestValidator, ClaimClientsUpdated>();
它将在自定义声明中添加前缀"client_",因此声明将是访问令牌中的"client_name":"value"
.
It will add prefix "client_" to custom claims , so claim will be "client_name": "value"
in access token .
这篇关于在IdentityServer4和ASP.NET Core中检索客户端名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!