Identity Server - 将自定义参数添加到来自令牌端点的 JSON 响应

vis*_* os 6 identityserver3 identityserver4

I\xe2\x80\x99ve 需要在 Identity Server 令牌端点的令牌响应中添加自定义成员。

\n

预期响应示例:

\n
{\n"access_token": "XXXXXXXXXXXXXXX",\n"token_type": "bearer",\n"expires_in": 3600,\n"scope": "patient/Observation.read patient/Patient.read",\n"patient": 123,\n"refresh_token":"XXXXXXXXXXXXXXXXX"\n}\n
Run Code Online (Sandbox Code Playgroud)\n

我想在响应中添加范围、患者参数,即使它存在于访问令牌中。

\n

任何关于这方面的指导都会非常有帮助!

\n

Cam*_*teh 5

对于 Identity Server 4,您可以通过实现 ICustomTokenRequestValidator 接口在令牌响应中添加自定义参数。

public class CustomTokenRequestValidator : ICustomTokenRequestValidator
{
    public Task ValidateAsync(CustomTokenRequestValidationContext context)
    {
        context.Result.CustomResponse =
          new Dictionary<string, object> {{ "patient", "alice"}};
        return Task.CompletedTask;
    }

    public CustomTokenRequestValidator()
    {
        
    }
}
Run Code Online (Sandbox Code Playgroud)

另外不要忘记在启动时在configureServices方法中注册依赖项。您可以在添加 IdentityServer 服务后附加 .AddCustomTokenRequestValidator<>({pass-in-name-of-class-implementing})。


Vid*_*ius 4

TokenResponse 由于模型的静态性质,开箱即用的配置是不可能的。

话虽如此,IdentityServer4 具有极强的可扩展性,因此您可以在技术上创建自己的实现ITokenResponseGenerator和自定义模型,以TokenResponse实现此行为。

但是,不推荐这样做,因为您似乎正在尝试解决某些其他系统无法处理相当标准的 JWT 的缺点。