Handling LOGOUT in form based Authentication

A Form based authentication is implemented in a web application and authenticated user is able to login into an application. While signing out, a user can be successfully redirected to Home page when the session and authentication cookie are cleared using Session.Abandon() and FormAuthentication.Signout() methods.
However, when an anonymous user hits browsers “back button”,  again the previous page from which authenticated user has signed out is visible but when some action is performed on the page, like clicking on a link or a button, it again redirects them back to Home page.
Many must have faced above problem and hope below solution resolves your problem.
Add below code in your logout button,

Session.Clear();                                 //this will clear session
Session.Abandon();                          //this will Abandon session
FormsAuthentication.SignOut();
Response.Cache.SetExpires(DateTime.Now);
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.Cache.SetNoStore();
Response.Redirect("Home.aspx");
In the Page_Load() method on the page (page where you have Logout button), add below lines of code,
Response.Cache.SetExpires(DateTime.UtcNow.AddMinutes(-1)); 
Response.Cache.SetCacheability(HttpCacheability.NoCache); 
Response.Cache.SetNoStore();

No comments:

Post a Comment