【问题标题】:.Net Core 2.1 Web API using JWT and JWT Cookie at the same time.Net Core 2.1 Web API 同时使用 JWT 和 JWT Cookie
【发布时间】:2018-11-19 18:35:47
【问题描述】:

我想将 JWT 存储在 cookie 中,并使用它来验证用户或 HTTP 标头中的不记名令牌。

目前我只使用 HTTP-Auth 标头并且它正在工作。

我尝试像这样使用身份 Cookie 和 JwT:

[Authorize] //Cookie auth?!
[Authorize(AuthenticationSchemes = JwtBearerDefaults.AuthenticationScheme)]
public class ValuesController : ControllerBase

Startup.cs:

services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
.AddJwtBearer(...)

...

services.AddIdentity<ApplicationUser, IdentityRole>()

我还尝试在AddAuthentication() 中添加不同的方案。它不工作。

我的问题是如何同时为动作/控制器激活 JWT 和 ASP.NET 身份验证。

【问题讨论】:

  • 问题是什么?你被否决了,因为正如它目前所写的那样,很难准确地说出你在问什么。请澄清您的具体问题或添加其他详细信息以准确突出您的需求

标签: c# asp.net-core asp.net-core-webapi


【解决方案1】:

根据您的描述,我猜您的操作需要多个身份验证。

您可以添加多个身份验证架构,就像使用 AddJwtBearerAddIdentity 所做的那样,并在 AuthorizeAttribute 上选择多个架构。

[Authorize(AuthenticationSchemes = JwtBearerDefaults.AuthenticationScheme + ", " + Microsoft.AspNetCore.Identity.IdentityConstants.ApplicationScheme)]

【讨论】:

  • 是的,这就是我要找的。但这给了我一个错误:An attribute argument must be a constant expression, typeof expression or array creation expression of an attribute parameter type 我尝试将其包装在一个静态只读字符串中。也不行; [Authorize(AuthenticationSchemes = AuthType.Value)]
  • 哦,我真的很想知道如何将 JWT 存储在 cookie 中并将其用于授权 + 普通 Microsoft JWT 授权。但是结合 Identity + JWT 也应该没问题,只要两者同时工作。
  • 将其更改为"Bearer, Identity.Application" 并且它似乎工作:) 我只需要一种简化它的方法,所以我不必将[Authorize(AuthenticationSchemes = "Bearer, Identity.Application")] 放在每个动作/控制器之上。有什么想法吗?
  • @DoubleVoid 这里有一个选项:docs.microsoft.com/en-us/aspnet/core/mvc/controllers/…
猜你喜欢
  • 2019-02-04
  • 2019-03-18
  • 2019-06-02
  • 2020-02-11
  • 1970-01-01
  • 1970-01-01
  • 2020-07-04
  • 2019-02-26
  • 2020-10-30
相关资源
最近更新 更多