0

I am trying to implement login and logout in asp.net(web forms). In my web form I have two pages namely Default and Main. From Default page when I login with username and password it redirects to the Main page. When I press back button it directly redirects to the default page. For this I copied javascript code to my default page

<script type = "text/javascript" >
    function preventBack() { window.history.forward(); }
    setTimeout("preventBack()", 0);
    window.onunload = function () { null };

source from stackoverflow question

After login when I click on back button in my browser(chrome) first it shows the Default page and then it shows the Main page. i.e page blinks when I click the back button.

It shows main page successfully with the issue. What should I implement to stop showing the Default page when I click on back button

Update:

<script type = "text/javascript" >
    history.pushState(null, null, 'Default.aspx');
    window.addEventListener('popstate', function (event) {
        history.pushState(null, null, 'Default.aspx');
    });
</script>

I placed this code in default page

Community
  • 1
  • 1
ifaminsi
  • 183
  • 3
  • 20

3 Answers3

2

There is one important thing that you ought to know here.

One cannot disable the browser back button functionality only thing that can be done is prevent it.

You can't, in anyway diasble that button. What you can however do, is to put some logic and prevent that button from doing what it is meant to do.

Now for the script that you have shown, it should serve fine and the other thing to try here is to put a mediator page between your default and main page. So when you login, the control will flow to mediator page and it will then redirect to main page. Now when the user presses back button on the main page, the control will flow to mediator page which will again redirect the user to his main page.

The effect will be the same as your script, but putting a page can help you write Session handling code and some server side checks if you want.

But one thing is sure, the browser back button will be as it is.

Hope this helps.

Dhrumil
  • 3,221
  • 6
  • 21
  • 34
  • Is there any other possibility to stay in the same page with out adding mediator page..? – ifaminsi Nov 27 '15 at 09:49
  • There isn't. Because as a developer, you can't handle the browser back event. So people tend to use such techniques to come around that. Now for your last try, you can refer this link and try the solution that this guy has posted. http://stackoverflow.com/questions/19926641/how-to-disable-back-button-in-browser-using-javascript – Dhrumil Nov 27 '15 at 09:52
  • When I press back button it should stay back in the same page but when I follow the above link it redirects to the page with HTTP 404 error – ifaminsi Nov 27 '15 at 10:12
  • Did you try using the full script with proper page names ? – Dhrumil Nov 27 '15 at 10:14
  • Yes please check my question I updated with code. Its not working properly – ifaminsi Nov 27 '15 at 10:22
  • Thanks Now I placed my code in Main page it worked successfully – ifaminsi Nov 27 '15 at 10:24
  • Cool. Glad that it helped ! Thanks. – Dhrumil Nov 27 '15 at 10:25
  • Also please help me how to maintain session. (i.e When the user closes the form with out clicking logout, when he opens the page it should be in the main page. It should not redirect to the sign in page) – ifaminsi Nov 27 '15 at 10:31
  • That particular scenario is generally handled itself. To start with the basics, you can try configuring the session state and timeouts in your web.config and also put some session related code onto your pages. You can also explore cookies and querystring sessions if you like. Its a wide topic and best covered on the internet. – Dhrumil Nov 27 '15 at 10:41
  • Here are some : http://www.codeproject.com/Articles/416137/Understanding-Session-Management-Techniques-in-ASP , http://www.c-sharpcorner.com/UploadFile/freelance91/ASPNETstatemanagementtechniques01012007212655PM/ASPNETstatemanagementtechniques.aspx , http://www.dotnetfunda.com/articles/show/61/state-management-in-aspnet – Dhrumil Nov 27 '15 at 10:55
0

As Matt said you can't disagree the back behaviour. However you can check if the user is logged in and redirect them to the main page easily.

All you need to do is, on the default page check if the user is logged in, if they are then redirect them to the main page, this way even if the user clicks back, they will be taken back to the main page. And also the can go to the Default page after logging out.

Don
  • 6,632
  • 3
  • 26
  • 34
  • This won't likely work out of the box as the default page might be cached and loaded as such. – Asons Nov 27 '15 at 10:00
  • My main issue is after logged in from default page, when the user clicks back button it should not redirect to default page(i.,e sign in page). I am not trying to disable back button just I want to stay back in the main page – ifaminsi Nov 27 '15 at 10:14
0

Another way could be using location.replace("...."), which replace the existing document and user can't "go back" using back button as the page doesn't exist in the url history.

Src: https://developer.mozilla.org/en-US/docs/Web/API/Location/replace

Asons
  • 84,923
  • 12
  • 110
  • 165
  • This post's answers will point you into a good direction: http://stackoverflow.com/questions/6990729/simple-ajax-form-using-javascript-no-jquery – Asons Nov 27 '15 at 10:25