asp.net-mvc 注销后,如果浏览器后退按钮按下,则返回上一个屏幕

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19315742/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 03:12:30  来源:igfitidea点击:

After logout if browser back button press then it go back last screen

asp.net-mvcasp.net-mvc-4razor

提问by Karan Prince

Hello I am developing a solution in MVC in first time so I am facing a big issue, When I logout from my application(mvc razor web application) it displays login page, but if i press browser back button it displays last screen, i don't want this, i want if i press back button it still display same login page. here is my code for logout

您好,我是第一次在 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");
    }

回答by AthibaN

I had this problem a while ago, disabling the cache for the entire application solved my problem, just add these line to the Global.asax.csfile

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

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

Hope this helps.

希望这可以帮助。

回答by Murali Murugesan

You need to add the cache METATag for all the last page you visited

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

So add this for all the pages, by making a CustomAttribute like [NoCache]and decorate

所以为所有页​​面添加这个,通过创建一个像[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();
    }
}

Or try it with javascript on the page like

或者在页面上尝试使用javascript

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

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

回答by ravenx30

Easiest way for a MVC 5 Application is:

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

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

Above each of the Controller Methods you don't want to Cache. Or if you are using .Core the following works:

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

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

Have a nice day!

祝你今天过得愉快!

回答by Nemi Amin

you can use onClick event of javascript to prevent browser back. for example:

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

<a onclick="noBack()" href="#">Logout</a>

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

Hope this will help you, Happy Coding!

希望这会帮助你,快乐编码!

回答by Anup Shetty

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

回答by Amit Rana

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