I am working on a ASP.NET application. I want to prevent the user from viewing the previous page using the back button in the browser after logging out from the application.
Why do you want to prevent the back button from working. Do avoid things that hinder the normal action of a user with the browser.
Instead you can check for Session availability in the page. If Session is not available then redirect the user to another page.
Anyways here is an article on this subject.
put this code in the page, where you want to prevent.
HttpContext.Current.Response.Cache.SetCacheability(System.Web.HttpCacheability.NoCache);
Plus you need to put extra check for your user session
I agree with phoenix... But one thing is the page would get already displayed when back button fires, from the cache and hence you can check for session validity only during postback... and remember, isPostBack will be true in that scenario.
You can use the following code to set the page expiry. This if included in a page, will display a page expired message when the user hits back button after going out of the page. But beware, he can always hit refresh and can perform the post back again.
Caution : using it in every other page, will frustrate the application users...
Response.AppendHeader("Pragma", "no-cache");
Response.AppendHeader("Cache-Control", "no-cache");
Response.CacheControl = "no-cache";
Response.Expires = -1;
Response.ExpiresAbsolute = new DateTime(1900, 1, 1);
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Thank You for all your replies.
Code:
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.Cache.SetExpires(DateTime.Now.AddSeconds(-1));
Response.Cache.SetNoStore();
Response.AppendHeader("Pragma", "no-cache");
i have used this code in page load event. Its working fine.