How to prevent users from switching to the previous page?

I am using ASP.NET MVC (latest version).

Imagine you have 2 pages:

Page-1: "Enter data" → Page-2: "Thank you"

After sending page-1, you are redirected to page-2.

My goal: . I want to make sure that you cannot return to page-1 when you click the back-to-back button as soon as you go to page-2. Instead, I want you to stay on page-2 (or click on page-2 every time you click the back button).

I tried all different things. Below is just a simplified pseudo code ...

[NoBrowserCache]
public ActionResult Page1(int userId)
{
   var user = GetUserFromDb(userId);
   if (user.HasAlreadySubmittedPage1InThePast)
   {
      // forward to page 2
      return RedirectToAction("Page2", routeValues: new { userId = userId });
   }

   var model = new Page1Model();

   return View("Page1", model);
}

[NoBrowserCache]
[HttpPost]
public ActionResult Page1(Page1Model model)
{
   var user = GetUserFromDb(model.UserId);
   if (user.HasAlreadySubmittedPage1InThePast)
   {
       // forward to page 2
       return RedirectToAction("Page2", routeValues: new { userId = model.UserId });
   }

   // Save posted data to the db
   // ...

   return RedirectToAction("Page2", routeValues: new { userId = model.UserId });
}

public class NoBrowserCache : ActionFilterAttribute
{
    public override void OnActionExecuting(ActionExecutingContext filterContext)
    {
        // Haha ... tried everything I found on the web here: 
        // Make sure the requested page is not put in the browser cache. 
        filterContext.HttpContext.Response.Cache.SetCacheability(HttpCacheability.NoCache);
        filterContext.HttpContext.Response.Cache.SetNoStore();
        filterContext.HttpContext.Response.Cache.AppendCacheExtension("no-cache");
        filterContext.HttpContext.Response.Cache.SetExpires(DateTime.Now);
        filterContext.HttpContext.Response.Expires = 0;
    }
}

, , . , "", -1 , . -2 .

?

, !

Btw: /. , Session.Abandon() - . - , javascript, .

EDIT 2017-5-12 @grek40, , -chaching-. [NoBrowserCache] -ActionFilterAttribute # . <head> _Layout.cshtml:

<meta http-equiv="Cache-Control" content="no-cache, no-store, must-revalidate">
<meta http-equiv="Pragma" content="no-cache">
<meta http-equiv="Expires" content="0">

, ( ). . - . Google Chrome v62, Firefox Quantum v57 Microsoft Edge v41 ( Win10). #

EDIT 2017-6-12 @grek40 : try : 0, : -1. . .

enter image description here

+4
5

, . javascript, .

-2 (, , , ):

<script type="text/javascript">

$(document).ready(function () {
    history.pushState({ page: 1 }, "title 1", "#nbb");
    window.onhashchange = function (event) {
        window.location.hash = "nbb";
    };
});

: javascript

. " " , .

+2

, javascript.

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

link1 link2.

+1

javascript :

history.forward();

. docs MDN. , ( BACK), . , ( ).

0
Response.Cache.SetCacheability(HttpCacheability.NoCache);  // HTTP 1.1.
Response.Cache.AppendCacheExtension("no-store, must-revalidate");
Response.AppendHeader("Pragma", "no-cache"); // HTTP 1.0.
Response.AppendHeader("Expires", "0"); // Proxies.
0

.

<script type="text/javascript">
                /*To retain on the same view on Back Click*/
                history.pushState(null, null, window.location.href);
                window.addEventListener('popstate', function (event) {
                    history.pushState(null, null, window.location.href);
                    event.preventDefault();
                    });
</script>
0

Source: https://habr.com/ru/post/1690080/


All Articles