Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Back button doesn't cause postback to a controller action in MVC

When I click the back button in IE10 or Chrome on Win7, it does not hit my break point in my MVC controller. The Network tab in IE developer's tools shows it had a 304 not modified and Fiddler doesn't capture the request.

I was expecting the post back, so I could do work in my controller. In my case, the bug is:

  1. Sign in
  2. make sure you are on the default page
  3. click the browser back button on the top left you'll now be back to the login screen
  4. sign in with your same credentials again when you do that - I get "The provided anti-forgery token was meant for user "", but the current user is "username".

I've tried putting this in my controller, without success:

this.HttpContext.Response.CacheControl = "private";
this.HttpContext.Response.Cache.SetMaxAge(TimeSpan.FromSeconds(0));
public ActionResult Index()
{
    // Get: /Home/Index
    if (this.User.Identity.IsAuthenticated)
    {
        // send the user to the GlobalAssetDashboard
        return this.RedirectToAction(
            "GlobalAssetDashboard",
            "Dashboard",
            new
                {
                    area = "DashboardArea"
                });
    }

    return this.View("Login");
}
 public ActionResult Login()
{
    // GET: /Home/Login
    if (this.User.Identity.IsAuthenticated)
    {
        // send the user to the GlobalAssetList
        return this.RedirectToAction(
            "GlobalAssetDashboard",
            "Dashboard",
            new
                {
                    area = "DashboardArea"
                });
    }

    return this.View("Login", new LoginModel());
}

Is there a way to force the postback or detect this and cause a refresh in JavaScript? Or maybe I have my controller methods implemented incorrectly?

like image 283
AlignedDev Avatar asked Apr 30 '13 20:04

AlignedDev


1 Answers

Typically caching rules like this aren't conditional upon the logic they perform, the URL as a whole is either cached or it isn't. In which case something as simple as this should suffice.

[OutputCache(NoStore=true, Duration=0)]
public ActionResult Login()
{

}

http://msdn.microsoft.com/en-us/library/dd492556(v=vs.108).aspx

like image 107
Nick Albrecht Avatar answered Sep 28 '22 07:09

Nick Albrecht