【问题标题】:After logout if browser back button press then it go back last screen注销后,如果按下浏览器后退按钮,则返回最后一个屏幕
【发布时间】:2013-10-19 09:24:52
【问题描述】:

您好,我是第一次在 MVC 中开发解决方案,所以我面临一个大问题, 当我从我的应用程序(mvc razor Web 应用程序)注销时,它会显示登录页面,但是如果我按下浏览器后退按钮,它会显示最后一个屏幕,我不想要这个,我想要如果我按下后退按钮它仍然显示相同的登录页面。 这是我的注销代码

public ActionResult Logout()
    {
        Session.Clear();
        Session.Abandon();
        Session.RemoveAll();

        FormsAuthentication.SignOut();


        this.Response.Cache.SetExpires(DateTime.UtcNow.AddMinutes(-1));
        this.Response.Cache.SetCacheability(HttpCacheability.NoCache);
        this.Response.Cache.SetNoStore();          

        return RedirectToAction("Login");
    }

【问题讨论】:

  • 没有理由同时调用 Clear AND Abandon。选择其中一个,否则您将来可能会遇到一些行为问题,例如意外触发会话特定事件。
  • 你说当你按下浏览器上的后退按钮并显示最后一个屏幕时,最后一个屏幕是什么以及从哪里调用注销?最后一个屏幕肯定是注销页面,因此没什么可看的?
  • 使用 System.Web.Mvc.OutputCache 属性来装饰您的控制器或其操作。

标签: asp.net-mvc asp.net-mvc-4 razor


【解决方案1】:

我前段时间遇到了这个问题,禁用整个应用程序的缓存解决了我的问题,只需将这些行添加到 Global.asax.cs 文件中

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

希望这会有所帮助。

【讨论】:

  • 这是否意味着你不能在整个应用程序中缓存任何东西
  • 不,但是当您回击时,您的控件会点击您的操作,而不是直接加载之前访问过的页面。
  • 禁用整个应用程序的页面缓存是一个糟糕的解决方案,这意味着对您的服务器的更多点击以再次执行该操作。这里的解决方案应该是一个孤立的,而不是整个应用程序。
  • 禁用后退按钮只是一种解决方法,我希望您知道使用NoCache 装饰所有动作或控制器与我的几乎相同!
  • AthibaN 的解决方案是最好的。谢谢你,先生。
【解决方案2】:

你需要为你最后访问的所有页面添加缓存META标签

所以为所有页​​面添加这个,通过制作像[NoCache] 这样的 CustomAttribute 并装饰

public class NoCacheAttribute : ActionFilterAttribute
{  
    public override void OnResultExecuting(ResultExecutingContext filterContext)
    {
        filterContext.HttpContext.Response.Cache.SetExpires(DateTime.UtcNow.AddDays(-1));
        filterContext.HttpContext.Response.Cache.SetValidUntilExpires(false);            
        filterContext.HttpContext.Response.Cache.SetCacheability(HttpCacheability.NoCache);
        filterContext.HttpContext.Response.Cache.SetNoStore();

        base.OnResultExecuting(filterContext);
    }
}


public class AccountController : Controller
{
    [NoCache]
    public ActionResult Logout()
    {
        return View();
    }
}

或者在页面上用javascript试试

<SCRIPT type="text/javascript">
    window.history.forward();
    function noBack() { window.history.forward(); }
</SCRIPT>

<BODY onload="noBack();"
    onpageshow="if (event.persisted) noBack();" onunload="">

【讨论】:

  • 但这会禁用所有操作方法的缓存吗?
  • @Murali Murugesan 当我在注销上方使用 [nocache] 时,它没有被触发,但当我在控制器类上方使用它时,它不会被触发
【解决方案3】:

MVC 5 应用程序最简单的方法是:

[OutputCache(NoStore = true, Duration = 0, VaryByParam = "None")]

在您不想缓存的每个控制器方法之上。或者,如果您使用的是 .Core,则以下工作:

[ResponseCache(Location = ResponseCacheLocation.None, NoStore = true)]

祝你有美好的一天!

【讨论】:

    【解决方案4】:
    protected void Application_BeginRequest()
            {
                Response.Buffer = true;
                Response.ExpiresAbsolute = DateTime.Now.AddDays(-1d);
                Response.Expires = -1500;
                Response.CacheControl = "no-cache";
                Response.Cache.SetNoStore();
            }
    
    Add [Authorize] filter on each controller
    

    【讨论】:

      【解决方案5】:
        protected void Application_BeginRequest()
          {
              Response.Cache.SetCacheability(HttpCacheability.NoCache);
              Response.Cache.SetExpires(DateTime.UtcNow.AddHours(-1));
              Response.Cache.SetNoStore();
              string emailAddress = null;
              var cookie =Request.Cookies[FormsAuthentication.FormsCookieName];
              // Nothing to do if no cookie
              if (cookie != null)
              {
                  // Decrypt the cookie
                  var data = FormsAuthentication.Decrypt(cookie.Value);
                  // Nothing to do if null
                  if (data != null)
                  {
                      // Deserialize the custom data we stored in the cookie
                      var o = JsonConvert.DeserializeObject<FormsAuthenticationTicketData>(data.UserData);
      
                      // Nothing to do if null
                      if (o != null)
                          emailAddress = o.EmailAddress;
                  }
              }
              SetupAutoFac(emailAddress);
          }
      

      【讨论】:

        【解决方案6】:

        您可以使用 javascript 的 onClick 事件来防止浏览器返回。 例如:

        <a onclick="noBack()" href="#">Logout</a>
        
        <Script type="text/javascript">
             window.history.forward();
             function noBack() { window.history.forward(); }
        </Script>
        

        希望这对您有所帮助,快乐编码!

        【讨论】:

          猜你喜欢
          • 2013-07-20
          • 2017-01-18
          • 2012-07-09
          • 2011-10-18
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2022-01-09
          • 2015-04-21
          相关资源
          最近更新 更多