IdentityServer4 中令牌端点中的自定义响应

Custom response in token endpoint in IdentityServer4

API 详情:.Net Core 3.1 REST API 使用 IdentityServer4 版本 3.1.3

我有很多 API 以指定格式发送响应。

例如在响应下方注册端点 returns:

{
    "responseCode": 0,
    "developerMessage": "Response code not specified.",
    "clientMessage": null,
    "data": {"id":123},
    "exception": null
}

我使用IdentityServer4 开发了身份验证服务器。 但是,我的令牌端点 returns 低于响应:

{
    "access_token": "eyJhbGciOiJSUzI1NiIsImtpZCI6Ik...",
    "expires_in": 1209600,
    "token_type": "Bearer",
    "refresh_token": "1u8_VOFHTaeqWEWd6R...",
    "scope": "offline_access api1"
}

现在的要求是 API 的所有端点都应该 return 以相同格式响应。

这意味着我需要更改令牌(或更多)端点的响应。

我查看了 ICustomTokenResponseGenerator 服务(提到 here),但它所做的只是向响应添加更多字段。 并且来自IdentityServer3

class CustomTokenResponseGenerator : ICustomTokenResponseGenerator
{
    public Task<TokenResponse> GenerateAsync(ValidatedTokenRequest request, TokenResponse response)
    {
        response.Custom.Add("custom_field", "custom data");      
        return Task.FromResult(response);
    }
}

但是,我想完全改变响应。

我可以使用任何其他服务来获得以下响应吗?

{
    "responseCode": 0,
    "developerMessage": "Response code not specified.",
    "clientMessage": null,
    "data": 
    {
        "access_token": "eyJhbGciOiJSUzI1NiIsImtpZCI6Ik...",
        "expires_in": 1209600,
        "token_type": "Bearer",
        "refresh_token": "1u8_VOFHTaeqWEWd6R...",
        "scope": "offline_access api1"
    },
    "exception": null
}

如您的 GitHub ticket 所述:

IdentityServer is an OAuth implementation - what you are suggesting would be incompatible with OAuth and thus is not supported by us.

If you need to change the complete payload to something custom - write some middleware to intercept the response.