当在ASP.NET Core中使用身份验证循环时,有几种可能的解决方法可以尝试:
public void ConfigureServices(IServiceCollection services)
{
// ...
services.AddAuthentication("MyCookieAuthenticationScheme")
.AddCookie("MyCookieAuthenticationScheme", options =>
{
options.Cookie.Name = "MyCookie";
options.Cookie.SameSite = SameSiteMode.None;
options.Cookie.SecurePolicy = CookieSecurePolicy.None;
options.ExpireTimeSpan = TimeSpan.FromMinutes(30);
options.LoginPath = "/Account/Login";
options.LogoutPath = "/Account/Logout";
// ...
});
// ...
}
public class AccountController : Controller
{
[HttpGet]
public IActionResult Login()
{
return View();
}
[HttpPost]
public async Task Login(LoginViewModel model)
{
if (ModelState.IsValid)
{
// 处理登录逻辑
// ...
// 返回重定向到受保护的资源的ActionResult
}
return View(model);
}
// ...
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
// ...
app.UseAuthentication();
// ...
app.UseMvc();
// ...
}
[Authorize]
public class MyProtectedController : Controller
{
// ...
}
通过检查和调整这些方面,您应该能够解决在ASP.NET Core中的身份验证循环卡住的问题。