注销操作方法从浏览器的缓存中提取数据

时间:2011-10-10 11:38:49

标签: c# asp.net asp.net-mvc asp.net-mvc-3

我有一个退出操作方法。我已经设置了no-cache,但它仍然从缓存中引入数据。这是我的方法:

public ActionResult Logout()
        {
            FormsAuthentication.SignOut();

            // clear authentication cookie
            HttpCookie cookie1 = new HttpCookie(FormsAuthentication.FormsCookieName, "");
            cookie1.Expires = DateTime.Now.AddYears(-1);
            Response.Cookies.Add(cookie1);

            // clear session cookie (not necessary for your current problem but i would recommend you do it anyway)
            HttpCookie cookie2 = new HttpCookie("ASP.NET_SessionId", "");
            cookie2.Expires = DateTime.Now.AddYears(-1);
            Response.Cookies.Add(cookie2);
            Session.Abandon();

            Response.Cache.SetCacheability(HttpCacheability.NoCache);
            Response.Cache.SetExpires(DateTime.Now.AddSeconds(-1));
            Response.Cache.SetNoStore();
            Response.AppendHeader("Pragma", "no-cache");

            return RedirectToAction("Login", "Account");
        }

我点击退出,我被重定向到登录页面。然后,当我点击浏览器后退按钮时,我仍然可以看到经过身份验证的页面,但是当我刷新时,我又被重定向到登录页面。谁能告诉我我做错了什么?

1 个答案:

答案 0 :(得分:2)

在您的操作中添加以下代码

   // to clear cache problems
        this.Response.Cache.SetExpires(DateTime.UtcNow.AddMinutes(-1));
        this.Response.Cache.SetCacheability(HttpCacheability.NoCache);
        this.Response.Cache.SetNoStore();