1

I am making a Kiosk application that multiple people may use one after the other. They need to put in some personal information and possibly even a credit card. Once they press 'submit' it will process the information and redirect them to a confirmation page.

However, I need to completely disable the back button in some way, so the next user cannot go back and take the previous client's information.

In my controller, I am using return RedirectToAction() not return View(). This helped the page not resubmit if the client presses the back button, but does not clear the form. I've also tried variations of:

window.history.forward();

This did not work AT ALL. When I had my controller return View(), this JavaScript prevented the user from going back, but flickered the previous page so you could see the information for about a second before it redirected back to the current view.

I have no issues completely disabling the back button for the entire application if that's the route I have to go. I just don't know HOW. I am not married to the idea of disabling the back button; if there are other solutions that will do this, I'm fine with trying those as well.

EDIT: There is a similar question that has been asked, but none of those solved the issue. Specifically, that question discusses disabling going back when log out occurs (clear session/clear cashe/etc). For my question, I cannot clear the session, because it will log the vendor out and the requirements documents specifically state that the vendor's session will be active for 8 hours, unless logged out.

HumanHickory
  • 464
  • 1
  • 6
  • 19
  • Possible duplicate of [disable browser back button javascript](https://stackoverflow.com/questions/22372185/disable-browser-back-button-javascript) – ArunPratap Aug 19 '19 at 12:28
  • Thanks for linking to it, but I did check that page prior to posting this and none of that seemed to work either. :-/ – HumanHickory Aug 19 '19 at 12:35

4 Answers4

2

I got the answer from a different source.

It seems the best way to do this is to change the history of the state.

let stateObject = {
    object: "blah"
}

history.pushState(stateObject, "", "../Home/Index")

So basically, if someone tries to go back in history, the browser remembers the last page being (in this case) /Home/Index, rather than whatever page you were actually on. So a back click will actually redirect back home.

I put this on the form page (not the confirmation page) in the script section. This is also helpful in case someone wants to go look up history.

HumanHickory
  • 464
  • 1
  • 6
  • 19
  • 1
    THIS! exactly what i needed! i needed the view itself to hide itself from browser history, rather than the view before or after it hiding the view- which is how the other answers handle it! – Heriberto Lugo Jul 18 '20 at 00:21
1

Disable Browser Back Button Script

The following JavaScript code snippet must be placed in the HEAD section of the View where the User must be prevented from going back.

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

Disable Browser Back Button Implementation in ASP.Net MVC

Login View

The HTML Markup of Login View consists of an HTML Anchor link to the Home View. The Disable Browser Back Button Script is placed in the HEAD section so that User cannot access the Login View using Browser Back button from Home View.

@{
    Layout = null;
}

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width"/>
    <title>Login</title>
    <script type="text/javascript">
        function preventBack() { window.history.forward(); }
        setTimeout("preventBack()", 0);
        window.onunload = function () { null };
    </script>
</head>
<body>
    <h3>Login</h3>
    <hr/>
    <a href="/Home/Home">Redirect to Home</a>
</body>
</html>

Home View

The HTML Markup of Home View consists of an HTML Anchor link to the Login View.

@{
    Layout = null;
}

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width"/>
    <title>Home</title>
</head>
<body>
    <h3>Home</h3>
    <hr/>
    <a href="/Home/Login">Redirect to Home</a>
</body>
</html>
Naveen
  • 1,441
  • 2
  • 16
  • 41
0

Add the following in your Global.asax

     protected void Application_BeginRequest()
    {
        Response.Cache.SetCacheability(HttpCacheability.NoCache);
        Response.Cache.SetExpires(DateTime.UtcNow.AddHours(-1));
        Response.Cache.SetNoStore();
    }

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. 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>
Nijin P J
  • 1,302
  • 1
  • 9
  • 15
  • So the one thing I cannot do is Abandon or clear the session. A vendor will log into the Kiosk and their clients will fill out the form. Clearing the session logs out the vendor, which is specifically part of the requirements document. It was a great idea though! Thank you! – HumanHickory Aug 19 '19 at 12:38
0

You could try preventing the files from being cached.

    response.setHeader("Cache-Control", "no-cache, no-store, must-revalidate");
    response.setHeader("Pragma", "no-cache");
    response.setDateHeader("Expires", 0);

If the user press the back button, it'll call your controller and you'll then have more controller over what is being seen.

the_lotus
  • 12,668
  • 3
  • 36
  • 53