Error unprotecting the session cookie. is a warning from ASP.NET Core's session middleware: the session cookie was protected with a data protection key the app no longer has. The request was served with an empty session and no error page. Keep the keys in a folder every start uses, and keep in mind that the session data itself needs a store that survives a restart too.

In the test, a persisted key folder made the warning go away, but the basket stayed empty after a restart, because AddDistributedMemoryCache keeps session data inside the process.

The error

...
warn: Microsoft.AspNetCore.Session.SessionMiddleware[7]
      Error unprotecting the session cookie.
      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)
...

That is the server console of the restarted app. The response itself was a normal 200:

PS> curl.exe -s -i -b sjar.txt http://127.0.0.1:15610/basket
HTTP/1.1 200 OK
...

Basket: (empty)

Why it happens

The test stored a value in the session while the app kept its keys in keys-a:

PS> curl.exe -s -c sjar.txt "http://127.0.0.1:15610/basket/add?item=notebook"
Basket holds notebook.
PS> curl.exe -s -b sjar.txt http://127.0.0.1:15610/basket
Basket: notebook

The app was then restarted on another key folder, keys-f. The session cookie named key 67c82b4c..., which the new key ring did not hold, so the session middleware logged the warning above and the request was served with an empty session.

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 -b sjar.txt http://127.0.0.1:15610/basket
Basket: (empty)

Started again on the original folder keys-a, the app logged no SessionMiddleware warning for the same cookie, so the key part is fixed by a key folder every start uses (PersistKeysToFileSystem). The basket was still empty: the session data had been in the memory cache of the process that was stopped. For a session that survives restarts the data has to live in a distributed cache outside the process; that was not tested here. AfLab, port 15610, the key folders, the routes /basket and /basket/add and the file sjar.txt 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, with AddDistributedMemoryCache(), AddSession(), app.UseSession(), two endpoints that write and read a session value, and data protection keys in a folder chosen on the command line. Requests went through curl 8.22.0 with a cookie jar.

Frequently asked

Why is my ASP.NET Core session empty after a restart?
In the test there were two reasons. A new key folder made the session cookie unreadable and logged Error unprotecting the session cookie, and the session data itself lived in AddDistributedMemoryCache, which a restart empties.
Does Error unprotecting the session cookie break the request?
No. In the test the request returned 200 with an empty session, and only the server console showed the warning.
How do I stop Error unprotecting the session cookie?
Keep the data protection keys in a folder every start of the app uses. In the test the warning did not appear when the app started again on the original key folder.

More decoded errors in the Fixes category. The same lost key signs users out silently in Unprotect ticket failed.