An exception occurred while processing the key element '...'. followed by System.PlatformNotSupportedException: This operation requires Windows 7 / Windows Server 2008 R2 or later. means a Linux container was given a data protection key ring written on Windows with ProtectKeysWithDpapi(). In the test the container marked the Windows key as ineligible and created a new one. Give the container a key ring of its own, on its own volume.

The app kept running: the log marked the Windows key as ineligible to be the default key, a new key file appeared beside it, and the app answered requests. Nothing protected on Windows was sent to the container in this test.

The error

fail: Microsoft.AspNetCore.DataProtection.KeyManagement.XmlKeyManager[24]
      An exception occurred while processing the key element '<key id="5f68107f-1cb1-40a1-a6ab-a4389fa56f51" version="1" />'.
      System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation.
       ---> System.PlatformNotSupportedException: This operation requires Windows 7 / Windows Server 2008 R2 or later.
         at Microsoft.AspNetCore.Cryptography.CryptoUtil.AssertPlatformIsWindows()
         at Microsoft.AspNetCore.DataProtection.XmlEncryption.DpapiXmlDecryptor..ctor(IServiceProvider services)
...
warn: Microsoft.AspNetCore.DataProtection.KeyManagement.DefaultKeyResolver[12]
      Key {5f68107f-1cb1-40a1-a6ab-a4389fa56f51} is ineligible to be the default key because its CreateEncryptor method failed after the maximum number of retries.
...

These are lines from docker logs. The fail block appeared 11 times and the warn line twice in the 435 lines the container wrote at start-up; no line said a key was not found. The page still answered HTTP/1.1 200 OK.

Why it happens

The key folder came from the Windows run of the No XML encryptor test, where the app used ProtectKeysWithDpapi(). A copy of that folder was mounted into a container of the same app on mcr.microsoft.com/dotnet/aspnet:10.0:

PS> docker run -d --name fix11-af-dpapi -p 127.0.0.1:15614:8080 -v "...\run13\dpapi-keys:/keys" fix11-af-lab --NoPersist false --Keys /keys

Reading the key needs the DPAPI decryptor, and its constructor refused to run on Linux, as the stack trace shows. The app then wrote a second key into the same folder:

PS> Get-ChildItem dpapi-keys -Name
key-59c4c2b5-302b-421e-a346-0a52d755bda7.xml
key-5f68107f-1cb1-40a1-a6ab-a4389fa56f51.xml

The fix

PS> docker run -d --name fix11-af-vol -p 127.0.0.1:15614:8080 -v fix11-af-keys3:/keys fix11-af-lab --NoPersist false --Keys /keys
...
PS> docker restart fix11-af-vol
fix11-af-vol
PS> curl.exe -s -i -b jar.txt -d "@body.txt" http://127.0.0.1:15614/book
HTTP/1.1 200 OK
...

Booked for Maria Garcia.

On a named volume of its own the container created its key, the log held only the No XML encryptor configured warning, and a form fetched before docker restart posted with 200 after it. That key ring is not encrypted: the warning means the key file sits on the volume in plain text, so the volume must be readable only by the app (see No XML encryptor configured). A ring that Windows and Linux could share, for example one protected with a certificate, was not tested here. AfLab, port 15614, the containers fix11-af-dpapi and fix11-af-vol, the image fix11-af-lab, the volume fix11-af-keys3, the route /book, the file jar.txt and the name Maria Garcia are the test's own; the bind-mounted path is shortened at the front.

How it was reproduced

A project from dotnet new web -n AfLab -f net10.0 on .NET SDK 11.0.100, no NuGet packages, published with dotnet publish -c Release -o out and copied into mcr.microsoft.com/dotnet/aspnet:10.0 (ASP.NET Core 10.0.13) on Docker Engine 29.9.3, with keys from PersistKeysToFileSystem(new DirectoryInfo(keysFolder)) and --Keys /keys on the command line. Containers were published on 127.0.0.1 only and removed with docker rm -f -v; the volume was removed by name.

Frequently asked

Can a Linux container read data protection keys protected with DPAPI?
Not in the test. The container logged An exception occurred while processing the key element with PlatformNotSupportedException for the DPAPI-protected key and created a new key instead.
Does the app stop when a key element cannot be processed?
No. In the test the container kept running and answered 200; the log marked the Windows key as ineligible to be the default key and a new key was created. Reading data protected on Windows was not tried.
How do I fix PlatformNotSupportedException from DpapiXmlDecryptor in Docker?
Give the container its own key ring on its own volume. In the test a named volume at /keys gave a clean key ring and a form posted after a restart.

More decoded errors in the Fixes category. The Windows side of this key ring is in No XML encryptor configured.