The antiforgery cookie token and request token do not match. means the hidden
form field came from one session and the antiforgery cookie from another. Both halves are
valid on their own; they were issued as a pair to different clients. Post the form with the
cookie that arrived together with it.
Whatever sends the request must keep the cookie set by the GET that rendered the form and send that same cookie with the POST. Fetch the form again if the cookie was lost or swapped.
The error
PS> curl.exe -s -i -b jar-a.txt -d "@body-b.txt" -H "Accept: text/plain" http://127.0.0.1:15610/book | Select-Object -First 12
HTTP/1.1 400 Bad Request
Content-Type: text/plain; charset=utf-8
...
Microsoft.AspNetCore.Http.BadHttpRequestException: Invalid anti-forgery token found when reading parameter "string name" from the request body as form.
---> Microsoft.AspNetCore.Antiforgery.AntiforgeryValidationException: The antiforgery cookie token and request token do not match.
at Microsoft.AspNetCore.Antiforgery.DefaultAntiforgery.ValidateTokens(HttpContext httpContext, AntiforgeryTokenSet antiforgeryTokenSet)
...
This is the response body of a minimal API form post in the Development environment; the
request sent Accept: text/plain and the body was this text. A second run of
the same pair without the header returned the same text, and the server console logged the
exception under fail: Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware[1].
Why it happens
GetAndStoreTokens sets an antiforgery cookie and returns a request token for
the hidden field. The test played two visitors with two cookie
jars, jar-a.txt and jar-b.txt, each fetching /form
once. It then posted the form body of the second visitor (body-b.txt) with the
cookie of the first. That mixed pair got the 400 above. Each visitor's own pair was
accepted: jar-a.txt with body-a.txt and jar-b.txt
with body-b.txt both returned "Booked for Maria Garcia.", from the same app
and the same key folder.
The fix
PS> curl.exe -s -b jar-a.txt -c jar-a.txt http://127.0.0.1:15610/form -o form.html
(wrote body-a.txt: name=Maria%20Garcia&__RequestVerificationToken=<token from form.html>)
...
PS> curl.exe -s -i -b jar-a.txt -d "@body-a.txt" -H "Accept: text/plain" http://127.0.0.1:15610/book
HTTP/1.1 200 OK
...
Booked for Maria Garcia.
The same cookie jar for the GET and the POST, and the body built from the hidden field of that same GET, gave 200 and "Booked for Maria Garcia.". Whatever client sends the form, the rule the test shows is one cookie store for the GET and the POST, and the field taken from that GET.
How it was reproduced
A project from dotnet new web -n AfLab -f net10.0 on .NET SDK 11.0.100, no
NuGet packages, running on ASP.NET Core 10.0.15 in Development on 127.0.0.1:15610: a
GET /form endpoint that writes the hidden field from
IAntiforgery.GetAndStoreTokens, a POST /book endpoint with
[FromForm] string name, and app.UseAntiforgery(). Requests went
through curl 8.22.0 with cookie jar files and the form body in a file written by a small
script from the hidden field. The jar and file names are the test's own.
Frequently asked
- What does antiforgery cookie token and request token do not match mean?
- The hidden form field and the antiforgery cookie were issued as a pair to different sessions. In the test, the cookie of one cookie jar was posted with the form field of another, and ASP.NET Core answered 400.
- How do I fix an antiforgery token mismatch in a test script?
- Keep one cookie store for the GET that renders the form and the POST that submits it, and build the body from the hidden field of that same GET. In the test, one curl cookie jar for both requests turned the 400 into a 200.
- Can both antiforgery tokens be valid and still not match?
- Yes. In the test each cookie jar's own pair was accepted with 200, and only the cookie of one jar with the form field of the other gave the do not match message.
More decoded errors in the Fixes category. When the cookie is missing altogether the message is a different one: The required antiforgery cookie is not present.