将我的网站从 .NET Core 2.2 迁移到 3.1.1 后,我的 api 端点突然开始尝试将我的 api 请求重定向到默认登录页面(/Account/Login?ReturnUrl=,我的任何路由中都没有该页面)。
我的 api 使用 JWT 承载身份验证方案和 JWT 挑战方案,但仍然发生了重定向。
services.AddAuthentication(options =>
{
options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
options.DefaultScheme = JwtBearerDefaults.AuthenticationScheme;
options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
})
Run Code Online (Sandbox Code Playgroud)
我终于找到了问题的解决方案,但我不知道为什么它实际上有帮助。
最初我的服务设置为:
services
.AddIdentity<IdentityUser, IdentityRole>()
.AddSignInManager()
.AddEntityFrameworkStores<CleWebToolsIdentityDbContext>();
Run Code Online (Sandbox Code Playgroud)
但这确实实现了重定向。
最终解决我的问题的是将它们设置为:
services
.AddIdentityCore<IdentityUser>()
.AddRoles<IdentityRole>()
.AddSignInManager()
.AddEntityFrameworkStores<CleWebToolsIdentityDbContext>();
Run Code Online (Sandbox Code Playgroud)
有人能告诉我这是怎么回事吗?
即使质询方案应该是 JWT,AddIdentity 方法如何导致重定向?