c# - ASP.NET Core 3 中的多种身份验证类型
问题描述
对于 Web 用户,我正在做基于 Cookie 的身份验证,而对于 IOT 设备,我正在做 JWT Auth。但我也有几个供 Web 和 IOT 用户使用的 API。
如果存在 Cookie,我希望它使用 Cookie/身份验证,如果存在承载,则使用 JWT 验证。实际上,它可能必须同时尝试。
我可以让它们单独工作,即 Cookie Auth 或 JWT Auth,但不能同时使用!
以下是我的 .Net Core 2.2 应用程序的代码片段,它完全符合我的需要。
services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme)
.AddCookie()
.AddJwtBearer(cfg =>
{
cfg.RequireHttpsMetadata = false;
cfg.SaveToken = true;
cfg.TokenValidationParameters = new TokenValidationParameters
{
ValidIssuer = Configuration["JwtIssuer"],
ValidAudience = Configuration["JwtIssuer"],
IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(Configuration["JwtKey"])),
};
});
我在 .Net Core 2.2 中也有这个。
services.AddMvc(config =>
{
var defaultPolicy = new AuthorizationPolicyBuilder(new[] { JwtBearerDefaults.AuthenticationScheme, IdentityConstants.ApplicationScheme, CookieAuthenticationDefaults.AuthenticationScheme })
.RequireAuthenticatedUser()
.Build();
config.Filters.Add(new AuthorizeFilter(defaultPolicy));
});
有人可以帮助我们解决 .Net Core 3 中的变化以及我如何解决这个问题。
解决方案
推荐阅读
- r - Shiny flexdashboard 中的 facet_grid 给出错误“构面变量必须至少有一个值”
- scala - Apache Flink 中的流式预测
- mysql - 选择表中的所有值加上返回 1/0 的列是否存在其他表中的记录
- android - Android小部件仅在调整大小时更新
- multinomial - Is MNL the right model to use when the choice options vary across observations?
- angular - 如何检查angular2下拉多选的所有选项
- c++ - 取消引用指针并立即为其分配变量导致分段错误
- javascript - 来自用户麦克风的 WAV 文件与来自文件的 WAV 文件:一些差异会导致错误,但这些有什么不同?
- tcl - 在 TCL 8.4 中复制多个文件
- java - Spring JDBCTemplate 执行不抛出异常