注销浏览器后退按钮然后返回最后一个屏幕

Kar*_*nce 21 asp.net-mvc razor asp.net-mvc-4

您好我是第一次在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");
    }
Run Code Online (Sandbox Code Playgroud)

Ath*_*baN 60

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

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

希望这可以帮助.

  • 它是一个糟糕的解决方案,可以禁用整个应用程序的页面缓存,这意味着服务器再次点击会再次执行操作.这里的解决方案应该是一个孤立的解决方案,而不是整个应用程序. (11认同)
  • AthibaN的解决方案是最好的.谢谢你,先生. (3认同)

Mur*_*san 11

您需要为META您访问的所有最后一页添加缓存标记

因此,通过制作CustomAttribute [NoCache]和装饰,为所有页面添加此内容

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();
    }
}
Run Code Online (Sandbox Code Playgroud)

或者在页面上使用javascript尝试

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

<BODY onload="noBack();"
    onpageshow="if (event.persisted) noBack();" onunload="">
Run Code Online (Sandbox Code Playgroud)


rav*_*x30 6

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

[OutputCache(NoStore = true, Duration = 0, VaryByParam = "None")]
Run Code Online (Sandbox Code Playgroud)

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

[ResponseCache(Location = ResponseCacheLocation.None, NoStore = true)]
Run Code Online (Sandbox Code Playgroud)

祝你今天过得愉快!