1.在注销方法中手动删除Cookies:
public IActionResult Logout()
{
HttpContext.SignOutAsync(CookieAuthenticationDefaults.AuthenticationScheme);
foreach (var cookie in Request.Cookies.Keys)
Response.Cookies.Delete(cookie);
return RedirectToAction("Index", "Home");
}
2.通过配置服务在注销时自动删除Cookies:
services.Configure(options =>
{
options.CheckConsentNeeded = context => true;
options.MinimumSameSitePolicy = SameSiteMode.None;
options.OnAppendCookie = cookieContext =>
CheckSameSite(cookieContext.Context, cookieContext.CookieOptions);
options.OnDeleteCookie = cookieContext =>
CheckSameSite(cookieContext.Context, cookieContext.CookieOptions);
});
public class AccountController : Controller
{
private readonly SignInManager _signInManager;
public AccountController(SignInManager signInManager)
{
_signInManager = signInManager;
}
[HttpPost]
[ValidateAntiForgeryToken]
public async Task Logout()
{
await _signInManager.SignOutAsync();
return RedirectToAction(nameof(HomeController.Index), "Home");
}
}
在Startup.ConfigureServices方法中,调用services.Configure