代码之家  ›  专栏  ›  技术社区  ›  user329540

MVC 4-注销后返回按钮问题

  •  3
  • user329540  · 技术社区  · 12 年前

    我读过很多帖子,人们也有类似的问题,但没有找到有效的解决方案。我有一个MVC 4网站,我不想从整个网站上删除缓存,因为我想缓存页面。当用户单击注销按钮时,它会成功注销并重定向到登录页面,但当用户单击后退按钮时,会显示以前查看过的“受限页面”,只有登录后才能看到。我知道这是因为浏览器缓存了页面客户端。我尝试了许多解决方案,但如前所述,它们都不起作用。目前我的注销代码如下:

        public ActionResult LogOff()
        {
    
            FormsAuthentication.SignOut();
            Session.Abandon();
            Session.Clear();
    
            // 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); 
    
            // Invalidate the Cache on the Client Side 
            Response.Cache.SetCacheability(HttpCacheability.NoCache);
            Response.Cache.SetExpires(DateTime.UtcNow.AddMinutes(-1));
            Response.Cache.SetNoStore();
    
            Response.AppendHeader("Pragma", "no-cache");
    
            // send an expired cookie back to the browser 
            var ticketExpiration = DateTime.Now.AddDays(-7);
            var ticket = new FormsAuthenticationTicket(
                1,
                // replace with username if this is the wrong cookie name 
                FormsAuthentication.FormsCookieName,
                DateTime.Now,
                ticketExpiration,
                false,
                String.Empty);
            var cookie = new System.Web.HttpCookie("user")
            {
                Expires = ticketExpiration,
                Value = FormsAuthentication.Encrypt(ticket),
                HttpOnly = true
            };
    
            Response.Cookies.Add(cookie); 
    
            return RedirectToAction("Login", "Account");
        }
    
    4 回复  |  直到 12 年前
        1
  •  4
  •   AthibaN    11 年前

    如果你想在所有页面上应用“浏览器返回时无缓存”行为,那么你应该将以下内容放在global.asax中。

    protected void Application_BeginRequest()
    {
        Response.Cache.SetCacheability(HttpCacheability.NoCache);
        Response.Cache.SetExpires(DateTime.UtcNow.AddHours(-1));
        Response.Cache.SetNoStore();
    }
    

    希望它能帮助到别人!

        2
  •  0
  •   RubbleFord    12 年前

    您可以使用浏览器窗口上的hashchange事件,在回发时触发ajax请求,这显然会在您注销时失败。从那里你可以触发浏览器做任何你喜欢的事情。

        3
  •  0
  •   Souvik Ghosh    7 年前

    将以下代码行添加到 全球.asax.cs 文件

    protected void Application_BeginRequest()
    {
        Response.Cache.SetCacheability(HttpCacheability.NoCache);
        Response.Cache.SetExpires(DateTime.UtcNow.AddHours(-1));
        Response.Cache.SetNoStore();
    }
    
        4
  •  0
  •   SHUBHASIS MAHATA    7 年前
        Response.ClearHeaders();
        Response.AddHeader("Cache-Control", "no-cache, no-store, max-age=0, 
        must-revalidate");
        Response.AddHeader("Pragma", "no-cache");
    
        Response.Cache.SetCacheability(HttpCacheability.NoCache);
        Response.Buffer = true;
        Response.ExpiresAbsolute = DateTime.Now.AddDays(-1);
        Response.Expires = 0;