Thursday, July 31, 2014

Prevent back button click after Logout the session in ASP.NET C#

Some time its happen after logging out when you are clicking on the back button its redirect to the previous page which one should open if the browser is holding the session. To prevent this a simple solution I found. Three lines of code that can solve your problem in a minute.

Code:

protected void Page_Load(object sender, EventArgs e)
{
      /* Starts here */
      Response.Cache.SetCacheability(HttpCacheability.NoCache);
      Response.Cache.SetExpires(DateTime.Now.AddDays(-1));
      Response.Cache.SetNoStore();
      /* Ends here */

      if (Session["User"] == null)
           Response.Redirect("login.aspx");
}

Copy the code and paste into your project and test it. It will redirect to your login page. Enjoy...

2 comments:

  1. How can i do it for after login. means when i login and redirecting to home page then back arrow should disappear and going on another page and again coming on home page then back arrow should disappear but previous means next arrow should appear. i have tried a code but in this after login it is disabling back arrow but after going on another page and coming on homepage back arrow is disabling but previous means next arrow is also disabling. I don't want like this:
    history.pushState(null, null, location.href);
    window.onpopstate = function () {
    history.go(1);
    };

    ReplyDelete

Popular Posts

Pageviews