如何向OAuth2客户端发送错误响应

7

Identity Server 4会重定向到AccountController进行登录, 一旦用户被验证后,将调用HttpContext.SignInAsync方法,然后执行返回ReturnUrl的Redirect操作。

但是,在某些情况下,需要将内部服务器错误发送回原始客户端,而不是在View中显示给最终用户。在这种情况下,我想发出标准的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 仓库 中发布此问题(并在那里链接此问题)。 - noseratio - open to work
1
在当前流程中,当发生这种情况时,用户是否会返回客户端?您能否谈谈这些“内部服务器错误”是什么,它们何时以及为什么会发生? - user4864425
你可以重写AuthorizeRequestValidator吗? - Vidmantas Blazevicius
2
“内部服务器错误”只是一种“假设”,实际上并没有发生。如果出现“内部服务器错误”,我该如何向中间件发送通知,以便它可以决定如何处理该过程。 - Rodney Bates
1个回答

4
这是不可能的,这是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");
}

这是受到这个答案的启发 问题


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接