无法使用FormsAuthentication.SignOut()从ASP.NET MVC应用程序注销

vij*_*ter 4 .net c# asp.net asp.net-mvc forms-authentication

我试图在ASP.NET MVC中实现Logout功能.

我为我的项目使用Forms身份验证.

这是我的退出代码:

FormsAuthentication.SignOut();
Response.Cookies.Clear();
FormsAuthenticationTicket ticket = 
    new FormsAuthenticationTicket(
        1,
        FormsAuthentication.FormsCookieName,
        DateTime.Today.AddYears(-1),
        DateTime.Today.AddYears(-2),
        true,
        string.Empty);

Response.Cookies[FormsAuthentication.FormsCookieName].Value = 
            FormsAuthentication.Encrypt(ticket); 
Response.Cookies[FormsAuthentication.FormsCookieName].Expires = 
            DateTime.Today.AddYears(-2);

return Redirect("LogOn");
Run Code Online (Sandbox Code Playgroud)

此代码将用户重定向到登录屏幕.但是,如果我通过在地址栏中指定名称来调用操作方法(或从地址栏下拉列表中选择上一个链接),我仍然可以在不登录的情况下访问安全页面.

有人可以帮我解决这个问题吗?

Pal*_*tir 6

这很奇怪......我只打了一次电话:FormsAuthentication.SignOut(); 它有效......

public ActionResult Logout() {
  FormsAuthentication.SignOut();
  return Redirect("~/");
}
Run Code Online (Sandbox Code Playgroud)