Cookies was not authenticated. Failure message: Unprotect ticket failed means
the authentication cookie was issued under a data protection key the app no longer has, so
the user is silently treated as signed out. Keep the keys in a folder that every start of
the app uses, and existing cookies keep working.
The client got no error page: the request went through as anonymous. Persist the keys with
PersistKeysToFileSystem in a folder every start uses. A new sign-in after the
key loss was not captured here.
The error
info: Microsoft.AspNetCore.Authentication.Cookies.CookieAuthenticationHandler[7]
Cookies was not authenticated. Failure message: Unprotect ticket failed
That is an info line in the server console, and the project template's
appsettings.Development.json sets "Microsoft.AspNetCore": "Warning",
so with the template's settings the console printed nothing at all for the request. The
key itself is named only at Trace level for data protection:
trce: Microsoft.AspNetCore.DataProtection.KeyManagement.KeyRingBasedDataProtector[6]
Key {67c82b4c-8171-4f8e-9c25-12382ecf7d83} was not found in the key ring. Unprotect operation cannot proceed.
What the client got was a normal page for an anonymous visitor:
PS> curl.exe -s -i -b jar.txt http://127.0.0.1:15610/me
HTTP/1.1 200 OK
...
You are not signed in.
Why it happens
The test signed in as maria while the app kept its keys in a folder
keys-a; /me answered "You are maria.". The app was then restarted
on an empty folder, created a new key there, and the old cookie named key
67c82b4c..., which the new key ring did not contain. The handler could not read
the ticket and reported the request as not authenticated, at info level.
To see the lines the test passed log levels on the command line, as development-only
settings. These are the two start lines exactly as run; the first showed the
Unprotect ticket failed line but not the key, the second showed both:
started pid 29380: dotnet bin\Debug\net10.0\AfLab.dll --urls http://127.0.0.1:15610 --environment Development --Keys keys-d --Logging:LogLevel:Microsoft.AspNetCore=Debug
started pid 8644: dotnet bin\Debug\net10.0\AfLab.dll --urls http://127.0.0.1:15610 --environment Development --Keys keys-e --Logging:LogLevel:Microsoft.AspNetCore.DataProtection=Trace --Logging:LogLevel:Microsoft.AspNetCore.Authentication=Information
The fix
...
var keysFolder = builder.Configuration["Keys"] ?? "keys-a";
...
builder.Services.AddDataProtection()
.PersistKeysToFileSystem(new DirectoryInfo(keysFolder))
.SetApplicationName("AfLab");
...
started pid 38284: dotnet bin\Debug\net10.0\AfLab.dll --urls http://127.0.0.1:15610 --environment Development --Keys keys-a
PS> curl.exe -s -i -b jar.txt http://127.0.0.1:15610/me
HTTP/1.1 200 OK
...
You are maria.
Started again on the folder that issued the cookie, the app read the same cookie and
/me answered "You are maria.". The lab has branches for other tests between the
lines shown. Keeping the folder outside the release folder, or on a volume shared by
several instances, is advice that was not tested here. The
Keys setting and the folder names are the test's own; the sign-in endpoint
signs in without a password and exists only in the test.
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, with
AddAuthentication().AddCookie(), a GET /login?user= endpoint that
calls SignInAsync, a GET /me endpoint that prints the user, and
data protection keys in a folder chosen by a command-line setting. Requests went through
curl 8.22.0 with a cookie jar file; the app was restarted four times on folders
keys-a, keys-c, keys-d, keys-e and back
to keys-a.
Frequently asked
- Why are users logged out after restarting an ASP.NET Core app?
- Their authentication cookies were protected with a data protection key the restarted app does not have. In the test, an app restarted on a new key folder treated a valid cookie as anonymous and logged Unprotect ticket failed.
- What does Unprotect ticket failed mean in ASP.NET Core?
- The cookie authentication handler could not decrypt the sign-in ticket in the cookie. In the test the cause was a missing key, named only in a Trace line: Key was not found in the key ring.
- Why do I not see Unprotect ticket failed in my logs?
- It is logged at Information level by the cookie handler, and the project template sets Microsoft.AspNetCore to Warning, so nothing was printed with the template's settings in the test.
More decoded errors in the Fixes category. Forms fail for the same reason with The antiforgery token could not be decrypted.