The required antiforgery request token was not provided in either form field
"__RequestVerificationToken" or header value "RequestVerificationToken". means the
request carried the antiforgery cookie but no request token in either of the two places
the message names: the __RequestVerificationToken form field or the
RequestVerificationToken header. This test sent the token in the header.
Take the request token from the page (here, the hidden field written by
GetAndStoreTokens) and send it with the POST, together with the cookie. In the
test, adding that one header to an otherwise identical request turned a 500 into a 200.
The error
PS> curl.exe -s -i -b jar.txt -d "@note.txt" -H "Accept: text/plain" http://127.0.0.1:15610/api/note | Select-Object -First 8
HTTP/1.1 500 Internal Server Error
Content-Type: text/plain; charset=utf-8
...
Microsoft.AspNetCore.Antiforgery.AntiforgeryValidationException: The required antiforgery request token was not provided in either form field "__RequestVerificationToken" or header value "RequestVerificationToken".
at Microsoft.AspNetCore.Antiforgery.DefaultAntiforgery.ValidateRequestAsync(HttpContext httpContext)
This is the response body in the Development environment; the request sent
Accept: text/plain and the body was this text. The status is 500 because the
endpoint calls ValidateRequestAsync itself and does not catch the exception.
Why it happens
The endpoint validates antiforgery by hand, because it does not bind a form:
...
app.MapPost("/api/note", async (IAntiforgery antiforgery, HttpContext context) =>
{
await antiforgery.ValidateRequestAsync(context);
return "Saved.";
});
...
The test fetched /form with a cookie jar, so the antiforgery cookie was in
jar.txt, read the hidden field into $token, and posted
note.txt (text=Call David Chen) with that cookie. The body had no
__RequestVerificationToken field and the request had no token header, and the
message names both places.
The fix
PS> curl.exe -s -b jar.txt -c jar.txt http://127.0.0.1:15610/form -o form.html
PS> $token = ([regex]'value="([^"]+)"').Match((Get-Content form.html -Raw)).Groups[1].Value
...
PS> curl.exe -s -i -b jar.txt -d "@note.txt" -H "Accept: text/plain" -H "RequestVerificationToken: $token" http://127.0.0.1:15610/api/note
HTTP/1.1 200 OK
...
Saved.
Both requests ran against one server process, with the same cookie jar, the same body and
the same Accept header; the only difference was the
RequestVerificationToken header, and the answer went from 500 to 200 and
"Saved.". The form field is the other place the message names; this test did not send it.
The project name AfLab, port 15610, the routes /form and
/api/note, the files jar.txt and note.txt and the
name David Chen are the test's own.
How it was reproduced
A project from dotnet new web -n AfLab -f net10.0 -o AfLab 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 and the POST /api/note endpoint
above. Requests went through curl 8.22.0 with a cookie jar and the body in a file; the
token was read from the saved page into a PowerShell variable, as shown.
Frequently asked
- How do I send the antiforgery token in a header in ASP.NET Core?
- Read the request token from the page and send it as the RequestVerificationToken header with the POST, together with the antiforgery cookie. In the test, the same request with and without that header answered 200 and 500.
- What is the default antiforgery header name in ASP.NET Core?
- The message printed in the test names it: RequestVerificationToken for the header and __RequestVerificationToken for the form field.
- Why does ValidateRequestAsync return 500 instead of 400?
- In the test the endpoint called ValidateRequestAsync without catching AntiforgeryValidationException, so the exception reached the developer exception page as an unhandled 500.
More decoded errors in the Fixes category. When the cookie is the half that is missing, the message is The required antiforgery cookie is not present.