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...

1 comments:

Post a Comment

Follow by Email

Popular Posts

Pageviews