简体   繁体   中英

Stop redirect page on back button click

I'm trying to remove history for a particular site. When I'm logout the page and then press back button, the page move in previous page but I want when any user logout and then press back button it will be on same page that is login page not go in previous page. I'm trying all method like session abandon, cache remove but my problem not solved. We can also use JavaScript.

protected void btnLogout_Click(object sender, EventArgs e)
{
    HttpContext.Current.Cache.Remove("");
    HttpContext.Current.Session.Clear();
    HttpContext.Current.Session.Abandon();
    Response.Redirect("../Login.aspx");
} 

You can try using javascript:

 <script type="text/javascript">
    //Logout clears all visited pages for Back Button
    function noBack() { window.history.forward(); }
    noBack();
    window.onload = noBack;
    window.onpageshow = function (evt) { if (evt.persisted) noBack(); }
    window.onunload = function () { void (0); }
</script>

If page is served using cache then do the below things to set the proper http headers

HttpContext.Current.Response.Cache.SetExpires(DateTime.UtcNow.AddHours(-1));
HttpContext.Current.Response.Cache.SetValidUntilExpires(false);
HttpContext.Current.Response.Cache.SetRevalidation(HttpCacheRevalidation.AllCaches);
HttpContext.Current.Response.Cache.SetCacheability(HttpCacheability.NoCache);
HttpContext.Current.Response.Cache.SetNoStore();

You've two options:

Prevent page from being cached Prevent user from going back to previous page after he logs out using JavaScript.

I would suggest the first one but if it doesn't work for you, go for the second option as mentioned here

From what I see, it looks like you are handling people logged on by session variables. If that is the case, then in the master page or a base page couldn't you check to make sure that person is logged on by checking the presence of a certain session variable? If they are logged on, continue as normal, if not then you could redirect them back to the logon page (or default) to make them log on?

In the code behind add the below code

protected override void OnPreRender(EventArgs e)
    {
        base.OnPreRender(e);
        string strDisAbleBackButton;
        strDisAbleBackButton = "<script language=\"javascript\">\n";
        strDisAbleBackButton += "window.history.forward(1);\n";
        strDisAbleBackButton += "\n</script>";
        ClientScript.RegisterClientScriptBlock(this.Page.GetType(), "clientScript", strDisAbleBackButton);
    }
    protected void Page_Init(object Sender, EventArgs e)
    {
        Response.Cache.SetCacheability(HttpCacheability.NoCache);
        Response.Cache.SetExpires(DateTime.Now.AddSeconds(-1));
        Response.Cache.SetNoStore();
    }

Hope the above code will work for you.

Use react and then, useEffect to have constant looping default such as /page=0 or /login added to the path say /accounts/login/login so when they click back it will just re-route to the same page as it can't go back, because its an infinite loop.

This question has been asked several times in stack overflow.A simple search would get you a solution. After logout you have to disable back button.for that you have to use

Page.Response.Cache.SetCacheability(HttpCacheability.NoCache)

Removing cache itself will stop navigating backwards.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM