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

Rod*_*tes 7 .net c# asp.net asp.net-web-api identityserver4

AccountController一旦验证了用户身份,Identity Server 4 便会重定向到用于登录的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
Run Code Online (Sandbox Code Playgroud)

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.
Run Code Online (Sandbox Code Playgroud)

may*_*ʎɐɯ 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");
}
Run Code Online (Sandbox Code Playgroud)

这是受到这个回答问题的启发。