cookiesdockerasp.net-coreasp.net-core-mvcasp.net-core-identity

Persist ASP.NET Core auth cookies between docker image launches


Each time a Docker image containing a .NET Core MVC web application starts up, all authentication cookies are invalidated, presumably due to a fresh machine key (which is used when signing the cookies) being generated.

This could traditionally be set via the <machineKey/> element in the web.config of a .NET app.

This link suggests that the DataProtection package would fit the bill, but the package seems to require the full fat framework.

What would be the correct way to ensure that every time a Docker image restarts it doesn't invalidate existing auth cookies?


Solution

  • You want to put the keys for data protection into a persistent and shareable location.

    If you're on AWS, AspNetCore.DataProtection.Aws allows to put the keyring on S3 with just a few lines of configuration code. Additionally you can leverage AWS KMS to encrypt the keys, which is especially useful to achieve consistent encryption algorithms, allowing to reuse the same key accross different operating systems which have different default encryption algorithms. The KMS option is also part of the same library.

    If you're on another platform than AWS, you'll need another library or mount a shared drive. But the concept of sharing the same location for the keys remains the same.