Click here to Skip to main content
15,881,204 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
I am using the following code for the logout button. It is working but there is a problem that it disable the back button for all the pages. i want to disable back button only on login page when user logouts..

please help me...

code on click event of logout button:
C#
protected void logout_button_Click(object sender, ImageClickEventArgs e)
    {
        HttpContext.Current.Response.Cache.SetCacheability(HttpCacheability.NoCache);
        HttpContext.Current.Response.Cache.SetNoServerCaching();
        HttpContext.Current.Response.Cache.SetNoStore();
        Session.Abandon();
        Session.Clear();
        Response.Redirect("~/login.aspx");
    }


javascript:

JavaScript
window.history.forward(1);
    document.attachEvent("onkeydown", my_onkeydown_handler);
    function my_onkeydown_handler() {
        switch (event.keyCode) {
            case 116: // F5;
                event.returnValue = false;
                event.keyCode = 0;
                window.status = "We have disabled F5";
                break;
        }
    }
Posted
Comments
_Amy 17-Jun-13 5:34am    
Please check my updated answer.

 
Share this answer
 
Comments
_Amy 17-Jun-13 4:58am    
+5! Nice links.
Please see my answer below, I've added some explanation in for this.
Thanks a lot _Amy... :)
Prasad_Kulkarni 17-Jun-13 6:05am    
Good links! +5
Thanks a lot Prasad_Kulkarni... :)
Use the below code on the page which you want to prevent to be landed using back button

XML
<script type="text/javascript">
  function preventBack() { window.history.forward(); }
  setTimeout("preventBack()", 0);
  window.onunload = function() { null };
</script>

<script language="JavaScript" type="text/javascript">
  javascript: window.history.forward(1);
</script>


Correction in your code:[Additional]

C#
Session.Abandon();
Session.Clear();\\Not required


Becuase Abandon has already thrown your session so no need to clear it.

Regards.. :)
 
Share this answer
 
v4
Comments
dimtdj 17-Jun-13 5:00am    
i have done what u said but its not solving my problem.
back button is still working for all the pages..
Prasad_Kulkarni 17-Jun-13 6:08am    
+5!
Thanks7872 17-Jun-13 6:27am    
Thanks Prasad..
Thanks7872 17-Jun-13 6:31am    
You have commented not working on all the answers,but all the codes are working.The answer i provided to you is the one i used to use. Remove all unnecessary code from head of your all the pages and put the above code only in your targeted page.Dont put this one in each and every page.
Quote:
I want to disable back button only on login page when user logouts

No, you cannot disable back button of browser in any conditions. You need to rethink your application logic so that it doesn't matter if the user presses the back button. Don't try and mess with the browser. It's either going to only work sporadically, and then, only for those with javascript enabled, and is subject to the whims of those who write the browsers.

The correct method is invalidate the cache, along with server side validation that the session is no longer valid if they try to resend data. To do this, please check Solution 1[^] answer given by Tadit.

[Edit 1]
Added code block.

You can add this javascript to login page.
HTML
<script type="text/javascript" language="javascript">
    function fnBackButton() {
        window.history.forward()
    }
    fnBackButton();
    window.onload = fnBackButton;
    window.onpageshow = function (evt) { if (evt.persisted) fnBackButton() }
    window.onunload = function () { void (0) }
</script>

Sometimes, javascript will not be enabled in client browser's. So I would suggest you to clear the cache in login page using c#. Use Page_Init even.
C#
protected void Page_Init(object sender, EventArgs e)
{
    Response.Cache.SetCacheability(HttpCacheability.NoCache);
    Response.Cache.SetExpires(DateTime.Now.AddSeconds(-1));
    Response.Cache.SetNoStore();
}



--Amit
 
Share this answer
 
v2
Comments
Thanks a lot _Amy... :) for referring my answer.

And my +5. :) Nice explanation.
_Amy 17-Jun-13 5:02am    
Thanks Tadit. :)
dimtdj 17-Jun-13 5:55am    
tried your code... its is not preventing going back after logout.
Prasad_Kulkarni 17-Jun-13 6:08am    
5'ed!
_Amy 17-Jun-13 6:14am    
Thanks Prasad. ;)
Try this:
JavaScript
<script type="text/javascript" language="javascript">
window.history.forward(1);
document.attachEvent("onkeydown", my_onkeydown_handler);
function my_onkeydown_handler()
{
  switch (event.keyCode)
  {
    case 116 : // F5;
       event.returnValue = false;
       event.keyCode = 0;
       window.status = "We have disabled F5";
       break;
  }
}
</script>


code behind:
C#
Session.Clear();
Session.Abandon();


Also refer:
Three ways to disable browser back button[^]
Use Javascript to restrict user from pressing back button in browser[^]
A Thorough examination of browser back button[^]
Simple solution[^]
Disabling browser's back functionality on sign out from Asp.Net[^]
Browser back button issue after logout[^]

..and lot more similar answer on CP QA Forum[^]
 
Share this answer
 
Comments
dimtdj 17-Jun-13 6:07am    
this the same code i ve use... ;)
Prasad_Kulkarni 17-Jun-13 6:09am    
..and what exactly the problem you're facing
dimtdj 17-Jun-13 6:12am    
my problem is this code is prevent back button from all the pages... i just want it to disable only on the login page after logout
Thanks7872 17-Jun-13 6:29am    
Important links...↑voted..!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900