【问题标题】:Returning 401 response in OpenID Connect server with ASOS在带有 ASOS 的 OpenID Connect 服务器中返回 401 响应
【发布时间】:2019-09-29 17:06:02
【问题描述】:

我按照Creating your own OpenID Connect server with ASOS 上的教程进行操作。一切正常,但如果用户输入错误的凭据,他将看到 400 响应状态代码。我想将此状态码更改为 401。这是我的 HandleTokenRequest 函数版本的代码:

public override Task HandleTokenRequest(HandleTokenRequestContext context)
{
    if (context.Request.IsPasswordGrantType())
    {
        return GrantResourceOwnerCredentials(context);
    }
        return base.HandleTokenRequest(context);
}

private async Task GrantResourceOwnerCredentials(HandleTokenRequestContext context)
{
    var userService = context.HttpContext.RequestServices.GetService<IUserService>();

    var user = await userService.AuthenticateAsync(context.Request.Username, context.Request.Password);
    if (user != null)
    {
        var identity = CreatePrincipal(Mapper.Map<UserModel>(user), null);
        var ticket = new AuthenticationTicket(identity, new AuthenticationProperties(), context.Scheme.Name);
        ticket.SetScopes(Consts.Scopes.Api1, Consts.Scopes.ApiOfflineAccess);

        context.Validate(ticket);
        return;
    }
    context.Reject(error: "invalid_grant", description: "The user name or password is incorrect.");
}

我看不到如何在这里传递响应代码。这种方法可行吗?

【问题讨论】:

  • 为什么要返回 401?据我所见,400 是这些错误的标准。
  • 似乎在规范中:openid.net/specs/…
  • 我被要求改变这一点,但在阅读了您的评论后,我认为我不应该这样做。谢谢@juunas

标签: asp.net-core openid-connect


【解决方案1】:

让我的评论成为答案。 根据我的经验,400 状态码是非常典型的。

而且它也在规范中:https://openid.net/specs/openid-connect-core-1_0.html#TokenErrorResponse

如果令牌请求无效或未经授权,授权 服务器构造错误响应。 Token的参数 错误响应在 OAuth 2.0 [RFC6749] 的第 5.2 节中定义。 HTTP 响应正文使用带有 HTTP 的 application/json 媒体类型 响应码 400。

【讨论】:

    猜你喜欢
    • 2016-12-18
    • 1970-01-01
    • 1970-01-01
    • 2012-05-15
    • 1970-01-01
    • 2012-07-02
    • 1970-01-01
    • 1970-01-01
    • 2017-06-18
    相关资源
    最近更新 更多