7
<script type="text/javascript">
    {
        function DisableBackButton() {
            window.history.forward()
        }

        DisableBackButton();

        window.onload = DisableBackButton;
        window.onpageshow = function (evt) { if (evt.persisted) DisableBackButton() }
        window.onunload = function () { void (0) }
    }
</script>

I am using the following code in the master page to diable the back button.

Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.Cache.SetExpires(DateTime.UtcNow.AddHours(-1));
Response.Cache.SetNoStore();

Response.ExpiresAbsolute = DateTime.Now.AddDays(-1d);
Response.Expires = -1500;
Response.CacheControl = "no-cache";
Page.Response.Cache.SetCacheability(HttpCacheability.NoCache);

I have a master page in that logout button is there once user click on that user will be redirected to logout page. This is working fine and once I am clicking on back button it is taking me to the last page I browsed. Even I tried with JavaScript.

I am creating timing out the session after 5 minutes. When session expires user will be redirected to session expiry page there also backbutton is taking me to the last page browsed.

krlzlx
  • 5,752
  • 14
  • 47
  • 55
Firoz Gandhi
  • 139
  • 1
  • 1
  • 15
  • Check this. it works fine http://geekswithblogs.net/Frez/archive/2010/05/18/back-button-issue-after-logout-in-asp.net.aspx – Syed Mohamed Feb 11 '15 at 15:39

7 Answers7

10

Here this JavaScript functionality will work in all browsers and prevent users navigating back to previous page by hitting on browser back button check below piece of JavaScript code

<script type="text/javascript" language="javascript">
     function DisableBackButton() {
       window.history.forward()
      }
     DisableBackButton();
     window.onload = DisableBackButton;
     window.onpageshow = function(evt) { if (evt.persisted) DisableBackButton() }
     window.onunload = function() { void (0) }
 </script>

We need to place above script in header section of a page wherever we need to prevent users navigate back to another page by using browser back button.

I will explain our requirement with an example I have two pages Defaul1.aspx and Default2.aspx now I will redirect from Default1.aspx page to Defaul2.aspx page. After come from Defaul1.aspx page to Default2.aspx if I try to navigate back to Default1.aspx page from Defaul2.aspx then I want prevent user navigate back to previous page (Defaul1.aspx). To achieve this functionality place above JavaScript function in header section of required page.

After add our JavaScript functionality to our page that code will be like this

<html xmlns="http://www.w3.org/1999/xhtml">
 <head runat="server">
    <title>Disable Browser Back buttons</title>
    <script type="text/javascript" language="javascript">

      function DisableBackButton() {
       window.history.forward()
      }
      DisableBackButton();
       window.onload = DisableBackButton;
       window.onpageshow = function(evt) { if (evt.persisted) DisableBackButton() }
        window.onunload = function() { void (0) }
     </script>
   </head>
   <body >
    <form id="form1" runat="server">
     <div>
        First Page
    </div> 
      <div>
         <asp:Button id="btnFirst" runat="server" Text="Go to First Page"  PostBackUrl="~/Default.aspx"  />
        <asp:Button ID="btnSecond" runat="server" Text="Go to Second Page"  PostBackUrl="~/Default2.aspx" />
        <asp:Button ID="btnThree" runat="server" Text="Go to Third Page" PostBackUrl="~/Default3.aspx" />
       </div>
    </form>
    </body>
   </html>

We can also achieve this by disabling browser caching in code behind write the following lines of code in Page_Init event or Page_Load event and don’t forgot to add namespace using System.Web; because HttpCacheability related to that namespace.

 protected void Page_Init(object sender, EventArgs e)
  {
      Response.Cache.SetCacheability(HttpCacheability.NoCache);
      Response.Cache.SetExpires(DateTime.Now.AddSeconds(-1));
      Response.Cache.SetNoStore();
   }

We need to place this code in a page wherever we need to disable browser back button

Mahesh
  • 8,694
  • 2
  • 32
  • 53
3
<script type="text/javascript" language="javascript">
    window.onload = function () {
        noBack();
    }
    function noBack() {
        window.history.forward();
    }
</script>
<body  onpageshow="if (event.persisted) noBack();">
</body>

Hello, You can do it like this,

Implement this code in master page

I have implemented this and it worked for me..

salah9
  • 502
  • 1
  • 10
  • 21
  • I placed this piece of code in master page and i backbutton is still taking me to the last browsed page one more thing here in the application backbutton shhould work and in logout page and session expiry page back buuton should not work.this is the requirement. – Firoz Gandhi Aug 02 '13 at 05:49
  • Insert this piece of code in logout page and expiry page... and this code is working fine in my application – salah9 Aug 02 '13 at 12:50
3
<script language="JavaScript">
this.history.forward(-1);

Msoni
  • 31
  • 1
1

Redirect to Logout.aspx page on clicking "logout" Add a new page as Logout.aspx with following body content.

        Please wait. You are Logging out.
        <asp:ScriptManager ID="ScriptManager1" runat="server">  
        </asp:ScriptManager>  
        <asp:Timer ID="Timer1" runat="server" Interval="1000" ontick="Timer1_Tick">  
        </asp:Timer>

add javascript as below

function noBack() {
            window.history.forward()
        }
        noBack();
        window.onload = noBack;
        window.onpageshow = function (evt) { if (evt.persisted) noBack(); }
        window.onunload = function () { void (0); }

Logout.aspx.cs

protected void Page_Load(object sender, EventArgs e)
    {
        Response.Cache.SetExpires(DateTime.UtcNow.AddMinutes(-1));
        Response.Cache.SetCacheability(HttpCacheability.NoCache);
        Response.Cache.SetNoStore();  
    }

    protected void Timer1_Tick(object sender, EventArgs e)
    {
        Response.Redirect("Login.aspx")); 
    }

Source : http://geekswithblogs.net/Frez/archive/2010/05/18/back-button-issue-after-logout-in-asp.net.aspx

Syed Mohamed
  • 1,339
  • 1
  • 15
  • 23
1

When user clicks logout button, you should write single line to clear the session.

Session.Abondon();

and navigate to logout page or login page. So that once user clicks logout button, he cannot go back becaause his session was cleared.

Learner
  • 1,286
  • 6
  • 34
  • 57
0

To disable the back button of the browser write the below code at master page header part as

<script language="JavaScript" type="text/javascript">
window.history.forward();              
</script>
shafi7468
  • 323
  • 1
  • 3
  • 15
0
 <script type="text/javascript">
        function DisableBackButton() {
            window.history.forward()
        }
        DisableBackButton();
        window.onload = DisableBackButton;
        window.onpageshow = function (evt) { if (evt.persisted) DisableBackButton() }
        window.onunload = function () { void (0) }
    </script>
Code
  • 679
  • 5
  • 9