Click here to Skip to main content
15,894,460 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
How to prevent back button after logout in asp.net?
Posted
Updated 18-Jan-19 23:15pm
Comments
olegtc 11-Apr-12 11:57am    
Does not work in FF

 
Share this answer
 
Comments
Tarun.K.S 9-Jun-11 4:52am    
Oh both our answers point to the same link! 5+
Please use this code in your Master Page Load Event. :)

C#
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");
                            }
        }


Ain Ronquillo
 
Share this answer
 
v2
Comments
Silverboy with Golden Heart 21-Mar-12 2:05am    
its working.
Silverboy with Golden Heart 21-Mar-12 2:06am    
can u pls explain y v use response.addheader?
venkateshkalyani 6-Jun-12 6:29am    
Beauty.....
Member 9042795 11-Jun-12 3:06am    
awesome...worked for me as well...
Ain Ronquillo 23-Aug-12 2:05am    
The AddHeader method adds a new HTML header and value to the response sent to the client. It does not replace an existing header of the same name. After a header has been added, it cannot be removed.
Use it in code behind

C#
HttpContext.Current.Response.Cache.SetCacheability(HttpCacheability.NoCache);
        HttpContext.Current.Response.Cache.SetNoServerCaching();
        HttpContext.Current.Response.Cache.SetNoStore();
        Session.Abandon();



Use it on head tag of aspx page


<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>
 
Share this answer
 
v3
Comments
Alok Kumar Sharma 11-Jun-11 8:53am    
Thanks
Alok Kumar Sharma
I would like you to see this excellent tip by Sandeep : http://www.codeproject.com/Tips/135121/Browser-back-button-issue-after-logout.aspx[^]

Hope it helped.
 
Share this answer
 
Comments
JustWorking 9-Jun-11 8:42am    
Check solution 3
Kill the session and check in each page if session is empty redirect to login page
 
Share this answer
 
Comments
Alok Kumar Sharma 11-Jun-11 8:26am    
Thanks,

This is alternate solution.
Member 10931224 17-Jul-14 3:49am    
how to check that session is empty
hi,,

best practice redirect to signout.aspx (create a new page for that) and on the page load write the code,

ie, clear all sessions and redirect to login page or what ever it is so when ever the person hit back button it will come to signout.aspx and redirect to login page


happy coding
 
Share this answer
 
Comments
anvas kuttan 10-Jun-11 0:08am    
why tou down voted this one if you dnt like this answer simply reject it
JustWorking 10-Jun-11 4:10am    
Same for solution 3 I don't know why downvote :( I 5'ved you and I hope you do same for me :)
anvas kuttan 10-Jun-11 4:15am    
yes i did
XML
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <script type="text/javascript">
        function changeHashOnLoad()
         {
            window.location.href += "#";
            setTimeout("changeHashAgain()", "50");
        }

        function changeHashAgain()
         {
            window.location.href += "1";
        }

        var storedHash = window.location.hash;
        window.setInterval(function ()
        {
            if (window.location.hash != storedHash)
             {
                window.location.hash = storedHash;
            }
        },
        50);
    </script>
</head>
<body onload="changeHashOnLoad(); ">
    <form id="form1" runat="server">
    <div>
    </div>
    </form>
</body>
</html>


rajendra patel jat
 
Share this answer
 
Comments
Member 10236682 25-Feb-14 2:40am    
too good and very absolute for requirment

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