该问题通常是由于使用HTTPS但未正确配置中间件导致的。我们可以在启动文件中添加中间件:
app.UseHttpsRedirection();
如果你不使用SSL,也可以通过重写OnRedirectToLogin事件来解决问题:
services.ConfigureApplicationCookie(options =>
{
options.Events = new CookieAuthenticationEvents
{
OnRedirectToLogin = ctx =>
{
if (ctx.Request.Scheme != "https")
{
ctx.Response.Redirect($"https://{ctx.Request.Host}{ctx.Request.Path}");
}
else
{
ctx.Response.Redirect(ctx.RedirectUri);
}
return Task.FromResult(0);
}
};
});
这将自动将Identity重定向到HTTPS地址。