Skip to main content

Keep ASP.NET Core Data Protection keys across builds

Show instructions for

ASP.NET Core Data Protection encrypts authentication cookies and antiforgery tokens with keys chosen by the application. By default, instances share any key created during preparation, and a fresh preparation can generate a different key. Losing the key can sign users out. To keep keys across builds, derive them from application settings during preparation or use an external store that each instance reads at startup.

From a secret in the application's settings​

builder.Services.AddDataProtection().UseConfiguredKeys();

This reads DataProtection:Keys:0, at least 32 random bytes in Base64, and refuses to start without it. For local development, use User Secrets:

dotnet user-secrets set "DataProtection:Keys:0" "$(openssl rand -base64 48)"
$key = New-Object byte[] 48
$random = [System.Security.Cryptography.RandomNumberGenerator]::Create()
try { $random.GetBytes($key) }
finally { $random.Dispose() }
dotnet user-secrets set "DataProtection:Keys:0" ([Convert]::ToBase64String($key))

To rotate, put the new key at Keys:0 and the old one at Keys:1. The first protects, all of them unprotect. Remove the old one once nothing it protected is in use. Configured keys are derived during preparation, so use a template prepared with the updated settings.

From the application's database​

With Entity Framework Core, add Isoplane.AspNetCore.DataProtection.EntityFrameworkCore at your SDK's version:

dotnet add package Isoplane.AspNetCore.DataProtection.EntityFrameworkCore --version VERSION

Add its DataProtectionKeys table to your context, as the underlying Microsoft.AspNetCore.DataProtection.EntityFrameworkCore package describes, then:

using Isoplane.AspNetCore.DataProtection.EntityFrameworkCore;

builder.Services.AddDataProtection().UseKeysFromDbContext<AppDbContext>();

Initialization loads the stored keys and can create a key if none is usable. Keys load when initialization finishes, so the table must exist by then. A migration in a Template.Initializing callback is fine. If that callback itself protects data, for instance by warming up a page with a form, load the keys first:

Template.Initializing += async () =>
{
await database.MigrateAsync();
await app.LoadDataProtectionKeysAsync();
await app.WarmupAsync("/");
};

The isoplane-webapp template is set up this way. For another store, UseKeysFrom takes two async functions that read and add keys. Return all stored key and revocation elements from the read function. Initialization creates and persists a key with the configured lifetime if no unrevoked active key remains valid beyond the renewal window.

Each new instance reads the store before Instance.Starting and uses the updated ring even for protectors created during warmup. An unavailable store or an unusable ring fails the instance's start. Keep old unrevoked keys in the store while their cookies or tokens remain valid. Running instances keep the ring they loaded. If a revocation must take effect for those instances, retire them or use an application-level check before accepting protected data.

note

ASP.NET Core's own stores, such as PersistKeysToDbContext, read keys synchronously and so fail here. The log shows An error occurred while reading the key ring, and protecting data then throws CryptographicException.