- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我在我的应用程序堆栈中使用 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();
}
http://localhost:5000/api/dashboard
上点击它并在我没有
Authorize
时得到预期的响应 Controller 上的属性。然后我添加了
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
<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/
当您使用 VS 2017 创建新的 Web API 并选择使用“工作或学校帐户”进行身份验证时,您将获得使用 JwtBearerAuthentication 库的代码。也可以手动编写 Web API
我在我的应用程序堆栈中使用 Firebase 和 dotnet 核心。 我正在使用 Firebase iOS API 生成 JWT 罚款,并且可以使用 jwt.io 确认结构符合预期且有效。的解析器。
我正在尝试使用 JWT用于身份验证机制 ASP.NET Core Web API项目。假设这个项目没有 MVC部分并且不使用 cookie 身份验证。我已经基于 this guide 创建了我的代码.
有人可以解释一下 UseOAuthBearerAuthentication 和 UseJwtBearerAuthentication 之间有什么区别吗? 为什么我应该使用“UseOAuthBearer
使用单个 asp.net(4.6.1) web 项目,显然我无法验证在同一服务器上生成的 jwt token 。 Startup.cs: var secret = Encoding.U
将代码库从 ASP 5 beta 7 更新到 RC1-final 后,我开始从 JwtBearer 中间件收到此异常 Unable to cast object of type 'Newtonsoft
我正在像这样使用 UseJwtBearerAuthentication app.UseJwtBearerAuthentication(options => { options.Authority
使用 ASP.NET Core 1.0 (rtm),有点 minimal verifiable sample我已经生成,我看到我生成的 JSON Web token 没有通过 .net core js
我已经使用 dot net core 1.1 开发了一个 web 应用程序。在我更新到 asp 核心 2.0 之前,它一直工作正常。然后在尝试使用该应用程序时,它报告此错误: InvalidOpera
当 401 和 403 发生时,我想使用 JSON 响应模型进行响应。例如: HTTP 401 { "message": "Authentication failed. The request m
我见过表明 IApplicationBuilder 的示例有一个扩展方法.UseJwtBearerAuthentication(Action options) . 这个SO question和 Asp
我是一名优秀的程序员,十分优秀!