asp.net - UseJwtBearerAuthentication 返回 401

标签 asp.net .net asp.net-web-api firebase-authentication .net-core

我在我的应用程序堆栈中使用 Firebase 和 dotnet 核心。

我正在使用 Firebase iOS API 生成 JWT 罚款,并且可以使用 jwt.io 确认结构符合预期且有效。的解析器。

设置

我已关注 this article设置 dotnet core 以根据请求自动设置主体。

我的启动文件的配置方法如下所示,在实践中 <project-id>部分被项目应用程序替换(我可以确认它与我在 jwt.io 上看到的解析 JWT 中的 aud 属性相同):

public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
    loggerFactory.AddConsole(Configuration.GetSection("Logging"));
    loggerFactory.AddDebug();
    loggerFactory.AddSerilog();

    app.UseJwtBearerAuthentication(new JwtBearerOptions
    {
        AutomaticAuthenticate = true,
        IncludeErrorDetails = true,
        Authority = "https://securetoken.google.com/<project-id>",
        TokenValidationParameters = new TokenValidationParameters
        {  
            ValidateIssuer = true,
            ValidIssuer = "https://securetoken.google.com/<project-id>",
            ValidateAudience = true,
            ValidAudience = "<project-id>",
            ValidateLifetime = true,
        },
    });

    app.UseMvc();
}

请求

我有一个 Controller 操作,我可以在 URL http://localhost:5000/api/dashboard 上点击它并在我没有 Authorize 时得到预期的响应 Controller 上的属性。然后我添加了 Authorize属性返回以便我可以开始测试 JWT 中间件。

我正在 Postman 中创建一个请求,如下图所示:
enter image description here

响应

如上图所示,响应是 401 Unauthorized .

服务器登录我的调试输出看起来像这样。
info: Microsoft.AspNetCore.Hosting.Internal.WebHost[1]
      Request starting HTTP/1.1 GET http://localhost:5000/api/dashboard  
Microsoft.AspNetCore.Hosting.Internal.WebHost:Information: Request starting HTTP/1.1 GET http://localhost:5000/api/dashboard  
info: Microsoft.AspNetCore.Authorization.DefaultAuthorizationService[2]
      Authorization failed for user: (null).
Microsoft.AspNetCore.Authorization.DefaultAuthorizationService:Information: Authorization failed for user: (null).
info: Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker[1]
      Authorization failed for the request at filter 'Microsoft.AspNetCore.Mvc.Authorization.AuthorizeFilter'.
Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker:Information: Authorization failed for the request at filter 'Microsoft.AspNetCore.Mvc.Authorization.AuthorizeFilter'.
info: Microsoft.AspNetCore.Mvc.ChallengeResult[1]
      Executing ChallengeResult with authentication schemes ().
Microsoft.AspNetCore.Mvc.ChallengeResult:Information: Executing ChallengeResult with authentication schemes ().
info: Microsoft.AspNetCore.Authentication.JwtBearer.JwtBearerMiddleware[12]
      AuthenticationScheme: Bearer was challenged.
Microsoft.AspNetCore.Authentication.JwtBearer.JwtBearerMiddleware:Information: AuthenticationScheme: Bearer was challenged.
info: Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker[2]
      Executed action Sured.Api.Controllers.DashboardController.Get (Sured.Api) in 20.2369ms
Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker:Information: Executed action Sured.Api.Controllers.DashboardController.Get (Sured.Api) in 20.2369ms
info: Microsoft.AspNetCore.Hosting.Internal.WebHost[2]
      Request finished in 37.6488ms 401 

问题

我一直在研究谷歌并浏览源代码,但我根本无法破解它。

谁能看到我在这里做错了什么?

为了更好地衡量,这是我项目中的所有依赖版本:
<PackageReference Include="Microsoft.AspNetCore" Version="1.1.2" />
<PackageReference Include="Microsoft.AspNetCore.Mvc" Version="1.1.3" />
<PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="1.1.2" />
<PackageReference Include="Serilog" Version="2.5.0" />
<PackageReference Include="Serilog.Enrichers.Environment" Version="2.1.2" />
<PackageReference Include="Serilog.Extensions.Logging" Version="1.4.0" />
<PackageReference Include="Serilog.Settings.Configuration" Version="2.4.0" />
<PackageReference Include="Serilog.Sinks.ElasticSearch" Version="5.3.0" />
<PackageReference Include="Serilog.Sinks.RollingFile" Version="3.3.0" />
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="1.1.2" />

最佳答案

默认情况下,该 key 似乎已被添加。您可能需要将 key 添加到帖子中。
key :Authorization值:Bearer eyjhbGciOij...这对于谷歌 API 帖子是必要的,以允许安全连接。仪表板会自动添加此 key 。

来源:
以前使用 Google API 的经验

关于asp.net - UseJwtBearerAuthentication 返回 401,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45517692/

相关文章:

asp.net-web-api - ASP.NET Web API IQueryable<T> 挑战

asp.net-web-api - 什么/为什么 Roslyn "needed"在 Asp.Net 的/bin 文件夹中

c# - 在不重新加载页面的情况下提交 TextBox 的值

asp.net - ADFS 4.0 : Received invalid Client credentials

c# - 如何在 C# 中将 List<dynamic> 转换为 List<OurClass>

c# - .NET:与正则表达式匹配时如何排除某些文件扩展名?

xml - 在保持默认 JSON 的同时强制 xml 在某些 Web API Controller 上返回

c# - jQuery post 数组 - ASP.Net MVC 4

c# - 生成随机字符串

c# - 在 Oracle 中生成 .Net 报价