Congratulations to all monthly winners of May 2013 !!! They have won INR 2900 cash and INR 27497 worth prize.
DotNetFunda.Com Logo
Twitter TwitterLinkedIn
YouTubeGoogle
 Online : 4504 |  Welcome, Guest!   Register  Login
 Home > Code Snippets > C# > Prevent Back Button after Logout in ASP.Net ...
Lakhwinder.ghuman

Prevent Back Button after Logout in ASP.Net

 Code Snippet posted by: Lakhwinder.ghuman | Posted on: 6/28/2011 | Category: C# Codes | Views: 13066 | Status: [Member] | Points: 40 | Alert Moderator   
Ads

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.

Best Regards
Lakhwinder Ghuman
Found interesting? Add this to:


 Responses

Lakn2
Posted by: Lakn2 | Posted on: 7/12/2011 | Level: Starter | Status: [Member] | Points: 10 | Alert Moderator 

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.

Thanks&Regards
LakshmiNarayana Nalluri.

Lakhwinder.ghuman
Posted by: Lakhwinder.ghuman | Posted on: 7/15/2011 | Level: Starter | Status: [Member] | Points: 10 | Alert Moderator 

java script is not suitable for all the cases as it raises the browser compatibility issues.

Best Regards
Lakhwinder Ghuman

Lakn2
Posted by: Lakn2 | Posted on: 7/15/2011 | Level: Starter | Status: [Member] | Points: 10 | Alert Moderator 

ok

Thanks&Regards
LakshmiNarayana Nalluri.

Akiii
Posted by: Akiii | Posted on: 11/21/2011 | Level: Bronze | Status: [Member] | Points: 10 | Alert Moderator 

Hi,
Good code but this isn't working for chrome browser ?
Can you tell why ?

Thanks and Regards
Akiii

Tanayarc
Posted by: Tanayarc | Posted on: 12/15/2012 | Level: Starter | Status: [Member] | Points: 10 | Alert Moderator 

hi,
thank u Lakhwinder Ghuman.
It's working

Thanks & Regards
Tanaya

Tanayarc
Posted by: Tanayarc | Posted on: 12/15/2012 | Level: Starter | Status: [Member] | Points: 10 | Alert Moderator 

hi,
thank u Lakhwinder Ghuman.
It's working

Thanks & Regards
Tanaya

>> Write Response - Respond to this post and get points

More codes snippets

About Us | Contact Us | The Team | Advertise | Software Development | Write for us | Testimonials | Privacy Policy | Terms of Use | Link Exchange | Members | Go Top
General Notice: If you find plagiarised (copied) contents on this page, please let us know the original source along with your correct email id (to communicate) for further action.
Copyright © DotNetFunda.Com. All Rights Reserved. Copying or mimicking the site design and layout is prohibited. Logos, company names used here if any are only for reference purposes and they may be respective owner's right or trademarks. | 6/18/2013 6:10:32 PM