【问题标题】:Custom response in token endpoint in IdentityServer4IdentityServer4 中令牌端点中的自定义响应
【发布时间】:2020-10-16 22:22:08
【问题描述】:

API 详细信息:.Net Core 3.1 REST API 使用 IdentityServer4 版本 3.1.3

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

例如注册端点返回以下响应:

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

我使用 IdentityServer4 开发了身份验证服务器。 但是,我的令牌端点返回以下响应:

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

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

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

我查看了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
}

【问题讨论】:

    标签: asp.net-core authentication oauth identityserver4 openid


    【解决方案1】:

    正如您在GitHub ticket 中解释的那样:

    IdentityServer 是一种 OAuth 实现 - 您的建议与 OAuth 不兼容,因此我们不支持。

    如果您需要将完整的有效负载更改为自定义 - 编写一些中间件来拦截响应。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-03-24
      • 2019-10-03
      • 2017-07-07
      • 2019-10-13
      • 2021-05-03
      • 2017-09-13
      相关资源
      最近更新 更多