Disable Browser Back Button

Background
Sometimes their is a need to disable the back button of the browser for security reasons so as to prevent the user from navigating between the pages by using the browser arrow buttons.
 
Problem
Suppose their is a Login page, when i login into the application, the page will be redirected to the page where I perform all my tasks, after doing all my work, I will logout from the application and the page again will get redirected to the Login page, at that time when i will click on the browser next arrow button it will allow me to show the next page that i have earlier used to Login  without the need of new login; so to avoid this problem we need to disable the browser button.
 
Now let us see how to do it...
open VS --create New Web site-add two web page...
 
After adding the two web pages, write the following javascript code.
  1. <script type ="text/javascript">  
  2.   
  3.     window.onload = window.history.forward(0);  //calling function on window onload
  4.     
  5. </script> 
Create the same page as above to navigate from the first page to the second page.. now the two pages with the entire code will look like the following:
 
 First.aspx Page
 
 
  1. <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>  
  2.   
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
  4.   
  5. <html xmlns="http://www.w3.org/1999/xhtml">  
  6. <head runat="server">  
  7. <script type ="text/javascript">  
  8.   
  9.     windowwindow.onload = window.history.forward(0);  
  10.     
  11. </script>  
  12.   
  13.     <title></title>  
  14. </head>  
  15. <body bgcolor="silver">  
  16.   
  17.   
  18.   
  19.     <form id="form1" runat="server">  
  20.       <h4>This is First  Page</h4>  
  21.       <br /><br /><br />  
  22.     <div>  
  23.    <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Goto Second Page" />  
  24.     </div>  
  25.      
  26.     </form>  
  27. </body>  
  28. </html> 
 SecondPage.aspx
  1. <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %>  
  2.   
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
  4.   
  5. <html xmlns="http://www.w3.org/1999/xhtml">  
  6. <head runat="server">  
  7. <script type ="text/javascript">  
  8.   
  9.     windowwindow.onload = window.history.forward(0);  
  10.     
  11. </script>  
  12.     <title></title>  
  13. </head>  
  14. <body bgcolor="silver">  
  15.     <form id="form1" runat="server">  
  16.     <div>  
  17.      <h4>This is second  Page</h4>  
  18.     </div>  
  19.     </form>  
  20. </body>  
  21. </html> 
 
Now run the application, the first page will look like the following:
 
 
 
Now click on the Goto Second Page button, it will be redirected to the next page as the following:
 
 
Now try to click on the above browser navigation button, now click on the above arrow button. here you will see that it will not directly allow you to navigate on the first page and from the first page to the second page.
 
Summary
It's a very small trick but it will play an important role in the security of the application, I hope this blog is useful, if you have any suggestion then please suggest me.