CryptographicException: The payload was invalid. from
IDataProtector.Unprotect appeared three times in this test, each time after one change: another purpose string, one changed character, and another application name
on the same key folder. Unprotect with the same purpose, the same application name and the
untouched string.
The message does not say which of the three it was, so check them in turn: the purpose
passed to CreateProtector, the SetApplicationName value of the app
that reads, and whether the string arrived exactly as it was protected.
The error
PS> curl.exe -s -i -H "Accept: text/plain" "http://127.0.0.1:15610/unprotect?payload=$payload&purpose=reports" | Select-Object -First 10
HTTP/1.1 500 Internal Server Error
Content-Type: text/plain; charset=utf-8
...
System.Security.Cryptography.CryptographicException: The payload was invalid. For more information go to https://aka.ms/aspnet/dataprotectionwarning
at Microsoft.AspNetCore.DataProtection.Cng.CbcAuthenticatedEncryptor.DecryptImpl(Byte* pbCiphertext, UInt32 cbCiphertext, Byte* pbAdditionalAuthenticatedData, UInt32 cbAdditionalAuthenticatedData)
...
The text is the response body of the test's /unprotect endpoint in Development,
with Accept: text/plain sent; the endpoint does not catch the exception, hence
the 500.
Why it happens
...
app.MapGet("/protect", (string text, string purpose, IDataProtectionProvider provider) =>
provider.CreateProtector(purpose).Protect(text));
app.MapGet("/unprotect", (string payload, string purpose, IDataProtectionProvider provider) =>
provider.CreateProtector(purpose).Unprotect(payload));
...
PS> $payload = curl.exe -s "http://127.0.0.1:15610/protect?text=Maria%20Garcia&purpose=invoices"
...
PS> curl.exe -s "http://127.0.0.1:15610/unprotect?payload=$payload&purpose=invoices"
Maria Garcia
The string protected with purpose invoices read back with
invoices. Read with purpose reports, it gave the error above. With
one character changed ($tampered = $payload.Substring(0, 40) + 'A' +
$payload.Substring(41)) and the right purpose, the message was the same. And an
instance of the app started on the same key folder with
SetApplicationName("OtherApp") instead of "AfLab" gave the same
message again for the untouched string and the right purpose. The same message, three times, each after one change.
The fix
started pid 41636: dotnet bin\Debug\net10.0\AfLab.dll --urls http://127.0.0.1:15610 --environment Development --Keys keys-a
PS> curl.exe -s "http://127.0.0.1:15610/unprotect?payload=$payload&purpose=invoices"
Maria Garcia
The app with application name AfLab, the key folder keys-a, the
purpose invoices and the untouched string read the value back. On the same key folder, a different SetApplicationName alone was enough to break the read in the test. AfLab, OtherApp, port 15610, the purposes, the routes /protect and /unprotect, the folder keys-a and the name Maria Garcia 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 in Development on 127.0.0.1:15610 on
Windows, with keys from PersistKeysToFileSystem in a folder of the test and the
two endpoints above. Requests went through curl 8.22.0; the payload was kept in a PowerShell
variable between calls, as shown.
Frequently asked
- What causes The payload was invalid in ASP.NET Core data protection?
- In the test three causes gave the same message: unprotecting with another purpose string, a payload with one character changed, and another application name on the same key folder.
- Does SetApplicationName matter for data protection?
- Yes in the test: an instance with SetApplicationName("OtherApp") on the same key folder could not read a string protected by the app named AfLab, and gave The payload was invalid.
- How is The payload was invalid different from key not found in the key ring?
- In this test the payload message came after a purpose change, a changed character and another application name, with the key folder unchanged. A changed key folder was not tried here; the antiforgery post linked below shows the key-not-found message for that case.
More decoded errors in the Fixes category. When the key itself is gone the message names it instead, as in The antiforgery token could not be decrypted.