对于Web用户,我正在执行基于Cookie的身份验证,对于IOT设备,正在执行JWT Auth。但是我也有Web和IOT用户都使用的几种API。
如果存在Cookie,则希望它使用Cookie /身份验证;如果承载存在,则它是JWT Auth。实际上,可能必须同时尝试。
我可以让他们单独工作,即Cookie身份验证或JWT身份验证,但不能同时使用!
以下是我来自.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中的更改以及如何解决此问题。