title: Login request validation token issue
body: <p>I have been going through the error logs of a development project and found the following error (name changed to protect the <strike>guilty</strike> innocent)-</p>
  
  <blockquote>
    <p>The provided anti-forgery token was meant for user "", but the current
    user is "admin".</p>
  </blockquote>
  
  <p>This was not an especially difficult issue to reproduce-</p>
  
  <ol>
  <li>Open the application at the login page</li>
  <li>Open a second window or tab in the <strong>same browser</strong> on the <strong>same computer</strong> to the login page before logging in</li>
  <li>Login in the first window (or indeed second, the order doesn't matter)</li>
  <li>Attempt to login in the remaining login window</li>
  </ol>
  
  <p>The stack trace is-</p>
  
  <blockquote>
    <p>System.Web.Mvc.HttpAntiForgeryException (0x80004005): The provided
    anti-forgery token was meant for user "", but the current user is
    "admin".    at
    System.Web.Helpers.AntiXsrf.TokenValidator.ValidateTokens(HttpContextBase
    httpContext, IIdentity identity, AntiForgeryToken sessionToken,
    AntiForgeryToken fieldToken)    at
    System.Web.Helpers.AntiXsrf.AntiForgeryWorker.Validate(HttpContextBase
    httpContext)    at System.Web.Helpers.AntiForgery.Validate()    at
    System.Web.Mvc.ValidateAntiForgeryTokenAttribute.OnAuthorization(AuthorizationContext
    filterContext)    at
    System.Web.Mvc.ControllerActionInvoker.InvokeAuthorizationFilters(ControllerContext
    controllerContext, IList`1 filters, ActionDescriptor actionDescriptor)
    at
    System.Web.Mvc.Async.AsyncControllerActionInvoker.&lt;>c__DisplayClass25.b__1e(AsyncCallback
    asyncCallback, Object asyncState)</p>
  </blockquote>
  
  <p>The login method signature is-</p>
  
  <pre><code>[HttpPost]
  [AllowAnonymous]
  [ValidateAntiForgeryToken]
  public ActionResult Login(LoginModel model, string returnUrl)
  {
  ...
  }
  </code></pre>
  
  <p>This is exactly the same as the signature for the method in a internet "ASP.NET MVC 4 Web Application" templated project, which indicates that Microsoft either felt the ValidateAntiForgeryToken was necessary/best practice, or simply added the attribute here because it was used everywhere else.</p>
  
  <p>Obviously there is nothing I can do to handle the problem <em>within</em> this method as it isn't reached, the ValidateAntiForgeryToken is a pre-request filter and it is blocking the request before it reaches the controller.</p>
  
  <p>I could check if the user is authenticated via Ajax before submitting the form and attempt to redirect to them if so, or simply remove the attribute.</p>
  
  <p>The question is this - I understand that the token is design to prevent requests from another site (CSRF) when the user <em>is already authenticated against your site</em> so on that basis <strong>is it an issue to remove it from a form which by definition will be used by unauthenticated users</strong>? </p>
  
  <p>Presumably the attribute in this instance is designed to mitigate malicious actors providing fake login forms for your application (although by the time the exception is thrown presumably the user has already entered his or her details which will have been recorded - but it might alert them something is wrong). Otherwise submitting incorrect credentials to the form from an external site will result in exactly the same result as on the site itself surely? I am not relying on client validation/sanitation to clean up potentially unsafe input.</p>
  
  <p>Have other devs come across this issue (or do we have unusually creative users) and if so how have you resolved/mitigated it?</p>
  
  <p><strong>Update:</strong> This issue still exists in MVC5, entirely intentionally, now with the error message "The provided anti-forgery token was meant for a different claims-based user than the current user." when using default template and Identity providers. There is a relevant question and interesting answer from Microsoft Developer Evangelist and Troy's fellow PluralSight author Adam Tuliper at <a href="https://stackoverflow.com/questions/9433083/anti-forgery-token-on-login-page">Anti forgery token on login page</a> which recommends simply removing the token.</p>