CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 10 of 10
  1. #1
    Join Date
    Sep 2004
    Posts
    265

    Unhappy Issues with browsers BACK and FORWARD buttons

    HI

    I have developed a web site. In the web site, the user can see view his details if he has logged in. In case user has not logged in, then the login screen is displayed, where the user logs in . In the details page, if the user clicks on the browser BACK button, he is taken back to login screen, but the password is not displayed.

    Now, the user can again click on the browser's FORWARD button to view the account details.

    I don't want this to be working this way. Even if the user clicks FORWARD button, he should not be able to proceed to details page without entering the pasword in login screen.

    How can I do this with ASP.NET ?

    Thanking in Advance

  2. #2
    Join Date
    Jun 2004
    Location
    Kashmir, India
    Posts
    6,808

    Re: Issues with browsers BACK and FORWARD buttons

    The moment you redirect the user to the Login Page you should be clearing the Session variables. And all the other pages before displaying any data should check the session variables and if they are empty the user should then be taken back to the Login Page.

    I am assuming that the once the user logs in a session variable is created for him/her which will be used through the application.

  3. #3
    Join Date
    Sep 2004
    Posts
    265

    Re: Issues with browsers BACK and FORWARD buttons

    I m clearing all the session variables, but if the user clicks the BACK button provided by the IE on top, to go back to the login screen and again if the user clicks the FORWARD button provided by IE, the user can access the details page without logging in.

    I want to avoid this,

    Is there any way to capture the browser's BACK and FORWARD button click events ?

  4. #4
    Join Date
    Jun 2004
    Location
    Kashmir, India
    Posts
    6,808

    Re: Issues with browsers BACK and FORWARD buttons

    Quote Originally Posted by rahulvasanth
    I m clearing all the session variables, but if the user clicks the BACK button provided by the IE on top, to go back to the login screen and again if the user clicks the FORWARD button provided by IE, the user can access the details page without logging in.

    I want to avoid this,

    Is there any way to capture the browser's BACK and FORWARD button click events ?
    Did you try doing what is suggested in my previous post?

  5. #5
    Join Date
    Jul 2006
    Posts
    19

    Re: Issues with browsers BACK and FORWARD buttons

    Clear your login session variable in pageload of your login.aspx.
    Check this session in all your other modules, if its nothing redirect to login.

    add these two lines of code in the pageload of each module you don't
    want to allow to go by browers back/forward button:

    Response.Cache.SetExpires(DateTime.Now.AddSeconds(-1))
    Response.Cache.SetCacheability(HttpCacheability.NoCache)



    Cheers,
    Andy

  6. #6
    Join Date
    Dec 2005
    Location
    Waterloo ON
    Posts
    545

    Re: Issues with browsers BACK and FORWARD buttons

    Actually, when user click "back" button to go to login webpage, server side code won't run. So it is useless to write code in page_load.

    The better way is to set cookie for that. Because javascript code will run when the webpage is showing by whatever way.

    the code in login.aspx:
    Code:
    window.onload = function(){	document.cookie='logged=;expires=Thu, 01-Jan-1970 00:00:01 GMT;';
    	document.getElementById('hidLogged').value = 'false';
    };
    window.onunload=function()
    {
    	if (document.getElementById('hidLogged').value == 'true')
    	{
    		document.cookie='logged=true;';
    	}
    };
    the code in default.aspx:
    Code:
    window.onload = function(){if(getCookie('logged') == null){window.location.assign('Login.aspx');return;};};
    You can try that.

  7. #7
    Join Date
    Jul 2006
    Posts
    19

    Re: Issues with browsers BACK and FORWARD buttons

    why is it useless?
    The code is executed once you load the page, so browsers caching for that
    page is no longer active.

    Absolutly no need for javascript.

  8. #8
    Join Date
    Sep 2004
    Posts
    265

    Re: Issues with browsers BACK and FORWARD buttons

    The page_load event is not triggered when I use the browser's BACK and FORWARD button

    I think the browser loads the pages from its memory.

  9. #9
    Join Date
    Jun 2004
    Location
    Kashmir, India
    Posts
    6,808

    Re: Issues with browsers BACK and FORWARD buttons

    Yes the Browser usually cache the Pages and when the same page is asked it will load it from the Cache.

  10. #10
    Join Date
    Dec 2005
    Location
    Waterloo ON
    Posts
    545

    Re: Issues with browsers BACK and FORWARD buttons

    Quote Originally Posted by AndyTheAce
    Clear your login session variable in pageload of your login.aspx.
    Check this session in all your other modules, if its nothing redirect to login.

    add these two lines of code in the pageload of each module you don't
    want to allow to go by browers back/forward button:

    Response.Cache.SetExpires(DateTime.Now.AddSeconds(-1))
    Response.Cache.SetCacheability(HttpCacheability.NoCache)



    Cheers,
    Andy
    Andy's method does work. However, how can you do when you have many pages under security control. For example, after login you wanna go back to the first page from the second page, can you?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured