Prevent Back Button after Logout in ASP.Net

Lakhwinder.ghuman
Posted by Lakhwinder.ghuman under C# category on | Points: 40 | Views : 108436
Prevent the Back option after Log Out:

To prevent the user to go back on your web page once the user has logged out , follow the procedure as:

Step 1 Set a Session in the Login Page after proper validation of User Id and Password. For e.g.
Session["LoginId"]=txtLoginId.Text;


Step 2 In the Page_Load event of your master page include the following code:
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
{
if (Session["LoginId"] == null)
Response.Redirect("frmLogin.aspx");
else
{
Response.ClearHeaders();
Response.AddHeader("Cache-Control", "no-cache, no-store, max-age=0, must-revalidate");
Response.AddHeader("Pragma", "no-cache");
}
}
}


Step 3 Do clear the session on the Logout button as:
 Session.Abandon();
Session.Clear();


By default when you press the browser's back button , it loads the page directly from the browser's cache but by implementing the code given in the step 2 , the page will be loaded and the page load event will fire.

Comments or Responses

Posted by: Lakn2 on: 7/12/2011 Level:Starter | Status: [Member] | Points: 10
good one but we have another way to prevent browser's back button using javascript that is simple try to avoid more lines of code.
Posted by: Lakhwinder.ghuman on: 7/15/2011 Level:Starter | Status: [Member] | Points: 10
java script is not suitable for all the cases as it raises the browser compatibility issues.
Posted by: Lakn2 on: 7/15/2011 Level:Starter | Status: [Member] | Points: 10
ok
Posted by: Akiii on: 11/21/2011 Level:Bronze | Status: [Member] | Points: 10
Hi,
Good code but this isn't working for chrome browser ?
Can you tell why ?

Thanks and Regards
Akiii
Posted by: Tanayarc on: 12/15/2012 Level:Starter | Status: [Member] | Points: 10
hi,
thank u Lakhwinder Ghuman.
It's working

Thanks & Regards
Tanaya
Posted by: Tanayarc on: 12/15/2012 Level:Starter | Status: [Member] | Points: 10
hi,
thank u Lakhwinder Ghuman.
It's working

Thanks & Regards
Tanaya
Posted by: Joganirakesh8225 on: 1/28/2014 Level:Starter | Status: [Member] | Points: 10
In this solution browser cache is clear if i not clear cache of browser any other solution for back buuton issue.
Actuall i have face problem when i click back button press in google chrome browser while pageload event is not call.


Thanks Regards
Rakesh Jogani
Posted by: Phurba on: 7/3/2014 Level:Starter | Status: [Member] | Points: 10
Thanks !

Login to post response