Javascript – How to clear browser history oan clear cache

asp.netcjavascript

I have a login page and a home page. Users can log in from the login page to the home page using a username and password. What I want to achieve is:

1) When user log out from home page, he/she should not be "logged in" by clicking the back or forward button in the browser.

2) It is sure that the browser will store a history of the home page. But when the user click on the history link in the home page available in the browser, it should be redirected to the login page instead of the home page.

I have tried clearing the cache by using the following code in page_load event in the home page:

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

But it is still not working. Any kind of help is highly appreciated.

Best Answer

Use follwing code in OnPrerender() event of home page . It worked for me.

  protected override void OnPreRender(EventArgs e)
        {
            base.OnPreRender(e);
            string sb;
            sb = "<script language=javascript>\n";
            sb += "window.history.forward(1);\n";
            sb += "\n</script>";
            ClientScript.RegisterClientScriptBlock(Page.GetType(), "clientScript", sb);
        }