我在应用程序堆栈中使用Firebase和dotnet核心。
我正在使用Firebase iOS API生成JWT罚款,并且可以使用jwt.io的解析器确认该结构是否符合预期且有效。
设置
我按照this article设置了dotnet core,以根据请求自动设置主体。
我的启动文件的configure方法如下所示,实际上<project-id>
部分由项目应用程序替换(我可以确认它与解析的JWT中的aud
属性相同,如jwt.io所示) :
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();
}
要求
我有一个控制器动作,当我在控制器上没有
http://localhost:5000/api/dashboard
属性时,可以点击URL Authorize
并获得预期的响应。然后,我又添加了Authorize
属性,以便可以开始测试JWT中间件。我正在邮递员中创建一个请求,如下图所示:
响应
如上图所示,响应为
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
问题
我一直在研究Google并浏览源代码,但我简直无法破解。
有人可以在这里看到我做错了吗?
出于良好的考虑,这是我项目中的所有依赖项版本:
<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" />
最佳答案
默认情况下,该密钥似乎已被添加。可能您可能需要将密钥添加到帖子中。
键:Authorization
值:Bearer eyjhbGciOij...
为了确保安全的连接,在Google API帖子中必须有此内容。仪表板会自动添加此键。
资源:
以前使用Google API的经验