Click here to Skip to main content
15,913,854 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
Dear Sir,

I am doing a project in asp.net. When I log out from my web application, and click the back button in the browser, then my web application goes back to the previous page.

How can I resolve this issue.??


Thanks in advance,


Dileep
Posted
Updated 20-Aug-12 6:50am
v2
Comments
Sergey Alexandrovich Kryukov 20-Aug-12 20:45pm    
You do not formulate any problem. I can guess that you don't want to get back to the login form, but everything should be formulated properly. And asking a question is required.
--SA

Try this code:
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>

and use this one in code behind
C#
Session.Clear();
Session.Abandon();


Alternative:
On code behind write:
C#
protected void Page_Load(object sender, EventArgs e)
   {      
     HttpContext.Current.Response.Cache.SetCacheability(HttpCacheability.NoCache);
     HttpContext.Current.Response.Cache.SetNoServerCaching();
     HttpContext.Current.Response.Cache.SetNoStore();
   }

..and on client side same JavaScript like above:
XML
<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>

Also have a look at following links :
1. Three ways to disable browser back button[^]
2. Use Javascript to restrict user from pressing back button in browser[^]
3. A Thorough examination of browser back button[^]
4. Simple solution[^]

Note: Many of the experts suggest not to disable expected browser behaviour. Instead make your pages handle the possibility of users going back. In other words redirect the user to certain page when user tries to press back button.
 
Share this answer
 
v2
 
Share this answer
 
XML
<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
<title>Untitled Page</title>
<script type = "text/javascript" >
function disableBackButton()
{
window.history.forward();
}
setTimeout("disableBackButton()", 0);
</script>
</head>
<body onload="disableBackButton()">
<form id="form1" runat="server">
<div>
This is First page <br />
<br />
Go to Second page
<br />
<br />
<asp:LinkButton ID="LinkButton1" runat="server"
PostBackUrl="~/Default2.aspx">Go to Second Page
</asp:LinkButton></div>
</form>
</body>
</html>


Code behind file for Default.aspx:

C#
protected override void OnPreRender(EventArgs e)
{
base.OnPreRender(e);
string strDisAbleBackButton;
strDisAbleBackButton = "<script language=javascript>\n";
strDisAbleBackButton += "window.history.forward(1);\n";
strDisAbleBackButton += "\n</script>";
ClientScript.RegisterClientScriptBlock(this.Page.GetType(), "clientScript", strDisAbleBackButton);
}
    protected void Page_Init(object Sender, EventArgs e)
    {
        Response.Cache.SetCacheability(HttpCacheability.NoCache);
        Response.Cache.SetExpires(DateTime.Now.AddSeconds(-1));
        Response.Cache.SetNoStore();
    }



Default2.aspx:

XML
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    second page
    </div>
    </form>
</body>
</html>


regards
sarva
 
Share this answer
 
Hi,

You have not posted your logout code but i guess you forgot to clear your session information.

Possible changes that you can do to resolve your issues are(if any of below missing),

1) Clear Session information and all other information on logout. if you are not clear session information properly then this behavior may occur.
2) You can even add javascript code in your page so user can't get back to previous page. (this will not work if user will disable javascript)
3) Check if user is really logon in your master page(if you have it), if not then redirect to login page.

Hope this information helps you,

Thanks
-Amit Gajjar
 
Share this answer
 
v2

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