在 Identity Server 4 中使用引用令牌传递附加数据

Tre*_*err 1 asp.net-core identityserver4

我在身份服务器上使用引用令牌,并希望将一些附加数据传递给客户端。

我知道如何通过在我的配置文件服务中设置声明来使用 JWT 执行此操作,但我找不到使用引用令牌执行类似操作的方法。理想情况下,我想将我的数据作为令牌 json 结果中的额外参数传递,如下所示:

{
    "access_token": "...",
    "expires_in": 3600,
    "token_type": "Bearer",
    "scope": "api1",
    "custom_property": "custom value"
}
Run Code Online (Sandbox Code Playgroud)

Nan*_* Yu 5

您可以实现(并注册)ICustomTokenRequestValidator可以帮助添加自定义响应参数的接口:

public class DefaultClientClaimsAdder : ICustomTokenRequestValidator
{
    public Task ValidateAsync(CustomTokenRequestValidationContext context)
    {
        context.Result.CustomResponse = new Dictionary<string, object>
        {
            {"hello", "world" }
        };

        return Task.FromResult(0);
    }
}
Run Code Online (Sandbox Code Playgroud)

Startup.cs在身份服务器应用程序中注册它:

services.AddTransient<ICustomTokenRequestValidator, DefaultClientClaimsAdder>();
Run Code Online (Sandbox Code Playgroud)

自定义属性将包含在令牌响应中:

在此输入图像描述