要强制所有用户注销(使用 cookie 身份验证)的解决方法,可以在 ASP.NET MVC 5 中执行以下步骤:
AccountController
中添加一个新的 LogoutAll
动作方法:[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult LogoutAll()
{
// 清除所有 cookie
foreach (var cookie in Request.Cookies.AllKeys)
{
Request.Cookies.Remove(cookie);
Response.Cookies.Remove(cookie);
}
// 执行用户注销逻辑
// ...
return RedirectToAction("Index", "Home");
}
Index.cshtml
)中创建一个注销链接,该链接将调用 LogoutAll
动作方法:@Html.ActionLink("Logout All", "LogoutAll", "Account")
Startup.cs
文件的 ConfigureServices
方法中,确保启用 cookie 身份验证:public void ConfigureServices(IServiceCollection services)
{
// ...
services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme)
.AddCookie();
// ...
}
这样,当用户点击注销链接时,将调用 LogoutAll
动作方法,清除所有 cookie 并执行自定义的用户注销逻辑。最后,用户将重定向到主页。