【问题标题】:MVC 4 - back button issue after logoutMVC 4 - 注销后的后退按钮问题
【发布时间】:2012-08-24 13:14:23
【问题描述】:

我阅读了许多帖子,其中人们遇到了类似的问题,但还没有找到有效的解决方案。我有一个 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");
    }

【问题讨论】:

  • 我不知道您是如何进行身份验证的。但是,不应该每个网站都检查您的数据库以确保用户在网站上有适当的身份验证吗?如果没有,它应该重定向他显示的东西。即使您的页面被缓存,您的脚本仍然会运行,并且存储在数据库中的用户会话应该显示他已注销。
  • 当然不行,你的过期码只影响当前请求是否被缓存。当前请求是您的注销代码,因此只有注销页面不会被缓存。您无法在缓存页面后追溯删除它们。

标签: asp.net-mvc asp.net-mvc-4 browser-cache back-button


【解决方案1】:

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

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

希望它可以帮助某人!

【讨论】:

    【解决方案2】:

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

    【讨论】:

      【解决方案3】:

      将以下代码行添加到 Global.asax.cs 文件中。

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

      【讨论】:

        【解决方案4】:
            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;
        

        【讨论】:

        • 虽然此代码可能会回答问题,但提供有关为什么如何此代码工作的附加信息可提高其长期价值。
        猜你喜欢
        • 2018-08-03
        • 2023-03-29
        • 1970-01-01
        • 1970-01-01
        • 2012-05-07
        • 1970-01-01
        • 2022-01-09
        • 2012-05-12
        • 1970-01-01
        相关资源
        最近更新 更多