【问题标题】:How to send an error response back to OAuth2 client如何将错误响应发送回 OAuth2 客户端
【发布时间】:2019-09-11 17:54:02
【问题描述】:

Identity Server 4 重定向到 AccountController 进行登录,一旦验证用户,就会调用 HttpContext.SignInAsync 方法,然后执行重定向到 ReturnUrl。

但是,在某些情况下,需要将internal server error 发送回原始客户端,而不是在视图中显示给最终用户。在这种情况下,我想发出标准的OAuth2 错误响应,但我没有看到这样做的方法。

更新:

我添加了更多信息。我指的是 OAuth 2.0 规范的这一部分。 Identity Server 可以这样做还是我必须从 RedirectUri 手动构建 URL。

基于此规范的 RedirectUri 示例如下:

例如,授权服务器通过发送以下 HTTP 响应来重定向用户代理:

   HTTP/1.1 302 Found
   Location: https://client.example.com/cb?error=access_denied&state=xyz

OAuth 2.0 规范的第 4.1.2.1 节规定:

    If the request fails due to a missing, invalid, or mismatching
redirection URI, or if the client identifier is missing or invalid,
the authorization server SHOULD inform the resource owner of the
error and MUST NOT automatically redirect the user-agent to the
invalid redirection URI.

If the resource owner denies the access request or if the request
fails for reasons other than a missing or invalid redirection URI,
the authorization server informs the client by adding the following
parameters to the fragment component of the redirection URI using the
"application/x-www-form-urlencoded" format.

【问题讨论】:

  • 也许您也可以将其作为问题发布到IdentityServer4 repo(并在此处链接此q)。
  • 当前流程中,出现这种情况时用户是否返回到客户端?您能否介绍一下这些“内部服务器错误”,它们发生的时间和原因?
  • 你可以覆盖 AuthorizeRequestValidator 吗?
  • “内部服务器错误”只是“假设”。它实际上并没有发生。如果出现“内部服务器错误”,我如何将通知发送回中间件,以便它决定如何处理该过程。

标签: c# asp.net .net asp.net-web-api identityserver4


【解决方案1】:

这是不可能的,这是 Identity Server 4 的设计意图。

如果你查看源代码https://github.com/IdentityServer/IdentityServer4/blob/master/src/IdentityServer4/src/Validation/Default/AuthorizeRequestValidator.cs#L146你会发现:

//////////////////////////////////////////////////////////
// check for valid client
//////////////////////////////////////////////////////////
var client = await _clients.FindEnabledClientByIdAsync(request.ClientId);
if (client == null)
{
    LogError("Unknown client or not enabled", request.ClientId, request);
    return Invalid(request, OidcConstants.AuthorizeErrors.UnauthorizedClient, "Unknown client or client not enabled");
}

这是受到question这个答案的启发。

【讨论】:

    猜你喜欢
    • 2019-11-17
    • 1970-01-01
    • 1970-01-01
    • 2015-09-24
    • 1970-01-01
    • 2020-02-11
    • 2014-09-13
    • 2019-05-31
    • 2015-05-11
    相关资源
    最近更新 更多