如何从ASP.NET Core 2.1 API获取401 Unauthorized的详细信息

问题描述 投票:3回答:2

我正在开发一个ASP.NET API(内置在Core 2.1中),当试图从测试应用程序调用它时,我得到一个401 Unauthorized。但是,据我所知,访问令牌很好。有没有办法从ASP.NET Core 2.1中的身份验证处理程序获取有关无效内容的更多详细信息?如果我能够知道令牌有什么问题,我应该能够找到解决问题的方法,但是现在我在黑暗中磕磕绊绊,只是尝试各种各样的事情(最有可能的是,我只是在制作它更差)。

c# authentication asp.net-core asp.net-core-2.1
2个回答
6
投票

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

对于你的错误,检查你是否在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.

-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

© www.soinside.com 2019 - 2024. All rights reserved.