Using an in-memory repository. Keys will not be persisted to storage. means ASP.NET Core found no place to keep its data protection keys and holds them in memory only, so every restart of the app makes new ones. In the test it came from a container run as a user id with no home folder. Give the app a key folder it can write, on a volume.

Call PersistKeysToFileSystem with a folder such as /keys, create that folder in the image owned by the user the container runs as, and mount a volume there. A form fetched before a restart then still posts.

The error

warn: Microsoft.AspNetCore.DataProtection.Repositories.EphemeralXmlRepository[50]
      Using an in-memory repository. Keys will not be persisted to storage.
warn: Microsoft.AspNetCore.DataProtection.KeyManagement.XmlKeyManager[59]
      Neither user profile nor HKLM registry available. Using an ephemeral key repository. Protected data will be unavailable when application exits.
warn: Microsoft.AspNetCore.DataProtection.KeyManagement.XmlKeyManager[35]
      No XML encryptor configured. Key {88135dca-91be-406b-91e6-f38904d57b1a} may be persisted to storage in unencrypted form.
...

These are the first lines of docker logs, printed at start-up, before Now listening on. The app answers requests normally; nothing on the page shows a problem until the container restarts.

Why it happens

The failing image started the app with the lab's --NoPersist true switch, which calls AddDataProtection() without a key location, and the container ran with --user 4321, a user id that has no account in mcr.microsoft.com/dotnet/aspnet:10.0:

FROM mcr.microsoft.com/dotnet/aspnet:10.0
WORKDIR /app
COPY out/ .
ENTRYPOINT ["dotnet", "AfLab.dll", "--NoPersist", "true"]
PS> docker run -d --name fix11-af-mem1 --user 4321 -p 127.0.0.1:15611:8080 fix11-af-lab

Inside it the home folder was the root of the filesystem:

PS> docker exec fix11-af-mem1 sh -c 'echo HOME=$HOME; id'
HOME=/
uid=4321 gid=0(root) groups=0(root)

The second warning says what followed: no user profile was available, so the keys lived in memory. The test fetched the form, restarted the container and posted the same cookie and field. Before the restart the post gave 200; after it, the app had a new key:

PS> docker restart fix11-af-mem1
fix11-af-mem1
PS> curl.exe -s -i -b jar.txt -d "@body.txt" http://127.0.0.1:15611/book
HTTP/1.1 400 Bad Request
Content-Length: 0
...

The fix

FROM mcr.microsoft.com/dotnet/aspnet:10.0
RUN mkdir /keys && chown 4321 /keys
WORKDIR /app
COPY out/ .
USER 4321
ENTRYPOINT ["dotnet", "AfLab.dll", "--Keys", "/keys"]
...
    builder.Services.AddDataProtection()
        .PersistKeysToFileSystem(new DirectoryInfo(keysFolder))
        .SetApplicationName("AfLab");
...
docker run -d --name fix11-af-mem3 -p 127.0.0.1:15612:8080 -v fix11-af-keys2:/keys fix11-af-lab:fix

With this image the log started with the No XML encryptor configured line only, the key file sat in the volume owned by uid 4321, and a form fetched before docker restart posted with HTTP/1.1 200 OK after it. The chown line matters: a first attempt that mounted a fresh named volume at /keys without it gave a 500 and Access to the path '/keys/9d1dedec-6bac-4c14-bd6c-a338eae38247.tmp' is denied. With the folder created and handed to uid 4321 in the image, the app could write there, and ls -ln /keys listed the key file as owned by 4321. The owner of the folder itself was not listed. The user id, image tags, container and volume names are the test's own.

The remaining No XML encryptor configured warning means the key file is plain XML in the volume; see the existing post linked below and the post on that warning.

How it was reproduced

A project from dotnet new web -n AfLab -f net10.0 on .NET SDK 11.0.100, no NuGet packages, with a minimal API form (GET /form with IAntiforgery.GetAndStoreTokens, POST /book with [FromForm] string name), published with dotnet publish -c Release and copied into mcr.microsoft.com/dotnet/aspnet:10.0 (ASP.NET Core 10.0.13) on Docker Engine 29.9.3. Containers were published on 127.0.0.1 only and removed with docker rm -f -v afterwards (each removal was run; the first one is in the capture); requests went through curl 8.22.0 with a cookie jar.

Frequently asked

What does Using an in-memory repository Keys will not be persisted to storage mean?
ASP.NET Core data protection keeps its keys in memory only, so each start of the app creates new keys. In the test, a form fetched before a container restart was rejected with an empty 400 after it.
Why does ASP.NET Core say neither user profile nor HKLM registry available in Docker?
In the test the container ran as a user id without an account in the image, so HOME was / and data protection found no profile folder for its keys.
How do I persist data protection keys when the container runs as a custom user?
Create the key folder in the Dockerfile and chown it to that user before the USER line, call PersistKeysToFileSystem with that folder, and mount a volume there. A fresh named volume without the chown gave access denied in the test.

More decoded errors in the Fixes category. For a container that runs as root and keeps its keys in its own filesystem, see Storing keys in a directory that may not be persisted outside of the container.