I have a form post that consistently gives me an anti-forgery token error.
Here is my form:
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
@Html.EditorFor(m => m.Email)
@Html.EditorFor(m => m.Birthday)
<p>
<input type="submit" id="Go" value="Go" />
</p>
}
Here is my action method:
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Join(JoinViewModel model)
{
//a bunch of stuff here but it doesn't matter because it's not making it here
}
Here is the machineKey in web.config:
<system.web>
<machineKey validationKey="mykey" decryptionKey="myotherkey" validation="SHA1" decryption="AES" />
</system.web>
And here is the error I get:
A required anti-forgery token was not supplied or was invalid.
I've read that changing users on the HttpContext will invalidate the token, but this isn't happening here. The HttpGet on my Join action just returns the view:
[HttpGet]
public ActionResult Join()
{
return this.View();
}
So I'm not sure what's going on. I've searched around, and everything seems to suggest that it's either the machineKey changing (app cycles) or the user/session changing.
What else could be going on? How can I troubleshoot this?
I don't know if you mean you are able to get the error on demand - or you're seeing it in your logs but in any case here's a way to guarantee an antiforgery token error.
Wait for it...
(For now I'm going to assume that this exact error message changed in MVC4 and that this is essentially the same message you're getting).
There's a lot of people out there that still double click on everything - this is bad! I just figured this out after just waking up so how this got through testing I really don't know. You don't even have to double click - I've got this error myself when I click a second time if the button is unresponsive.
I just removed the validation attribute. My site is always SSL and I'm not overly concerned about the risk. I just need it to work right now. Another solution would be disabling the button with javascript.
This can be duplicated on the MVC4 initial install template.
After help from Adam, I get the MVC source added to my project, and was able to see there are many cases that result in the same error.
Here is the method used to validate the anti forgery token:
My problem was that condition where it compares the Identity user name with the form token's user name. In my case, I didn't have the user name set (one was null, the other was an empty string).
While I doubt many will run into this same scenario, hopefully others will find it useful seeing the underlying conditions that are being checked.
You should prevent double form submission. I prevent this type of issue using code like this:
or
I just had a similar problem. I got:
What's of interest is that I tried debugging it and saw the token in both places I expected to find it in the controller
In reality I should have been looking here:
As Params contains more than just the form fields, it contains the QueryString, Form, Cookies and ServerVariables.
Once that red herring was out of the way I found the AntiForgeryToken was in the wrong form!
html error logger is not correct line.
you must check all loading value in page is not null.
Are you on one server or a web farm? If a single server, comment out your machineKey element in your web.config and try again as a base starting point. Any change? Also - can you think of any reasons your cookies would be getting cleared or expiring - they are required for this to work properly as well.