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

本文关键字:获取 数据 缓存 浏览器 操作方法 注销 | 更新日期: 2023-09-27 18:11:02

我有一个注销操作方法。我已经设置了无缓存,但仍然从缓存带来的数据。这是我的方法:

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");
        }

我点击登出,我被重定向到登录页面。然后,当我点击浏览器返回按钮时,我仍然可以看到经过身份验证的页面,但当我刷新时,我再次被重定向到登录页面。有人能告诉我我哪里做错了吗?

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

在您的actions中添加以下代码

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