The antiforgery token could not be decrypted. with the inner message
The key {...} was not found in the key ring means the form was rendered
while the app used one data protection key ring and was posted after it switched to
another. Keep the keys in one folder that survives a restart, and old forms post again.
Point AddDataProtection().PersistKeysToFileSystem(...) at a folder that every
start of the app uses. In the test, a form fetched before the restart then posted
normally.
The error
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 token could not be decrypted.
---> System.Security.Cryptography.CryptographicException: The key {67c82b4c-8171-4f8e-9c25-12382ecf7d83} was not found in the key ring. For more information go to https://aka.ms/aspnet/dataprotectionwarning
at Microsoft.AspNetCore.DataProtection.KeyManagement.KeyRingBasedDataProtector.UnprotectCore(Byte[] protectedData, Boolean allowOperationsOnRevokedKeys, UnprotectStatus& status)
...
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. The same lines appear in the server console under
fail: Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware[1]. In
a Production container test (keys kept in memory, container restarted between the GET and
the POST) the response was this:
HTTP/1.1 400 Bad Request
Content-Length: 0
...
Why it happens
In this test a token made while the app had one key was rejected once the app had another.
The test fetched /form with curl while the
app kept its keys in a folder called keys-a, stopped the app, started it again
with keys-b, and posted the saved cookie and field. The new start created a new
key in keys-b, and the key the token named, 67c82b4c..., was only
in keys-a:
PS> Get-ChildItem ..\AfLab\keys-a, ..\AfLab\keys-b | Select-Object -ExpandProperty FullName
...\AfLab\keys-a\key-67c82b4c-8171-4f8e-9c25-12382ecf7d83.xml
...\AfLab\keys-b\key-8b711269-29cd-4437-a2be-c12f2b80fb4b.xml
(The paths are shortened at the front.) A user who opens the form, waits through such a restart and submits sees the 400.
The fix
...
var keysFolder = builder.Configuration["Keys"] ?? "keys-a";
...
builder.Services.AddDataProtection()
.PersistKeysToFileSystem(new DirectoryInfo(keysFolder))
.SetApplicationName("AfLab");
...
PS> curl.exe -s -i -b jar.txt -d "@body.txt" -H "Accept: text/plain" http://127.0.0.1:15610/book
HTTP/1.1 200 OK
...
Booked for Maria Garcia.
With the app started again on the original folder keys-a, the very same saved
cookie and form body were accepted. The test showed one folder kept across restarts of
one app; keeping that folder outside the release folder, or sharing it between instances,
is advice that was not tested here. The lab picks the folder with a setting and has
branches for other tests between the lines shown. The Keys setting and the folder names are the test's own.
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: a GET /form endpoint that
writes the hidden field from IAntiforgery.GetAndStoreTokens, a
POST /book endpoint that binds [FromForm] string name, and
app.UseAntiforgery(). Every request went through curl 8.22.0 with a cookie jar
file and the form body in a file, against 127.0.0.1:15610.
Frequently asked
- Why does the antiforgery token could not be decrypted error appear after a restart?
- The token was protected with a data protection key that the restarted app no longer has. In the test the app started with a different key folder, created a new key there, and could not read the token made with the old one.
- How do I keep ASP.NET Core data protection keys across restarts?
- Call AddDataProtection().PersistKeysToFileSystem with a folder that every start of the app uses. In the test, an app started again on its original key folder accepted a form fetched before the restart.
- Why is the 400 for a bad antiforgery token empty in production?
- In the one Production run captured, a container that kept its keys in memory and was restarted between the GET and the POST, the answer was HTTP 400 with Content-Length 0. The Development runs returned the exception text.
More decoded errors in the Fixes category. For the same error after a container is replaced, see Storing keys in a directory that may not be persisted outside of the container.