【问题标题】:How-To get details of 401 Unauthorized from ASP.NET Core 2.1 API如何从 ASP.NET Core 2.1 API 获取 401 Unauthorized 的详细信息
【发布时间】:2018-07-24 14:18:56
【问题描述】:

我正在开发一个 ASP.NET API(内置于 Core 2.1),当尝试从测试应用程序调用它时,我收到 401 Unauthorized。但是,据我所知,访问令牌很好。有没有办法从 ASP.NET Core 2.1 中的身份验证处理程序中获取有关无效内容的更多详细信息?如果我能对令牌有什么问题有所了解,我应该能够找到解决方案来纠正它,但现在我在黑暗中蹒跚而行,只是尝试各种事情(很可能,我只是在做更糟)。

【问题讨论】:

    标签: c# authentication asp.net-core asp.net-core-2.1


    【解决方案1】:

    检查日志或服务器的事件查看器。

    对于您的错误,请检查您是否将app.UseAuthentication() 作为Configure() 中的第一个方法调用,甚至在UseMVC() 之前。

    如果您提到错误的子代码会有所帮助:

    401.1: Access is denied due to invalid credentials.
    401.2: Access is denied due to server configuration favoring an alternate authentication method.
    401.3: Access is denied due to an ACL set on the requested resource.
    401.4: Authorization failed by a filter installed on the Web server.
    401.5: Authorization failed by an ISAPI/CGI application.
    401.7: Access denied by URL authorization policy on the Web server.
    

    【讨论】:

    • 谢谢,这正是我的问题所在。
    • +1 在我将app.UseAuthentication() 移到Configure() 中的所有方法之上后,这对我有用
    • 将 UseAuthentication 移到 UseMvc 之上为我做了。
    • @FrqSalah 嘿,你能链接到你从哪里获得这些代码的信息吗?谢谢!
    • @andre_ss6 你可以在这里找到一切:docs.microsoft.com/en-us/previous-versions/iis/6.0-sdk/…
    【解决方案2】:

    您应该查看服务器日志。我可能是您在请求中传递令牌的方式。例如它可能是什么:

    我在做:

    GET https://localhost:44357/api/test 
    Headers:   
        Bearer: {ACCESS_TOKEN_VALUE}   
        Content-Type: application/json 
    

    当我应该做的时候:

    GET https://localhost:44357/api/test 
    Headers:   
        Authorization: Bearer {ACCESS_TOKEN_VALUE}
        Content-Type: application/json
    

    https://github.com/openiddict/openiddict-core/issues/577

    【讨论】:

      猜你喜欢
      • 2021-08-03
      • 2014-10-15
      • 2016-09-24
      • 1970-01-01
      • 1970-01-01
      • 2022-08-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多