Repository navigation
Data Protection and Shortened Certificate lifetimes #70074
|
So I use Data Protection in my MVC Core App and have a config section like this: So far so good. I am not aware of a component that can do this. |
Replies: 1 comment 2 replies
|
You probably don't need to follow the TLS rotation here at all. The certificate Data Protection uses is only an RSA key that wraps the key ring XML at rest. It's never presented to a client, so it doesn't need to come from a public CA. The shorter lifetimes you're adopting apply to publicly trusted TLS certificates. So the simplest fix is a dedicated, long-lived, self-signed certificate used only for Data Protection, kept separate from your Let's Encrypt certificates. For example: openssl req -x509 -newkey rsa:3072 -nodes -days 3650 -subj "/CN=MyApp DataProtection" -keyout dp.key -out dp.crt
openssl pkcs12 -export -inkey dp.key -in dp.crt -out dp.pfxOne thing to watch: pass it as an var cert = X509CertificateLoader.LoadPkcs12FromFile("dp.pfx", password); // .NET 9+; on older versions: new X509Certificate2("dp.pfx", password)
builder.Services.AddDataProtection()
.PersistKeysToFileSystem(new DirectoryInfo(keyRingPath)) // or whatever you use today
.ProtectKeysWithCertificate(cert);The two overloads behave differently:
If you ever do rotate it, the mechanism is the one you already have. Encrypt with the new certificate, and keep the old ones in (I verified this against the aspnetcore source but didn't run it.) |
"File not found" can mean two different things here, and the fix depends on which one you're seeing.
1.
System.IO.FileNotFoundException: the path itself is wrong.Under IIS the current directory is not your app folder, so a relative path like
"dp.pfx"won't resolve. Use an absolute path, or build one frombuilder.Environment.ContentRootPath.2.
CryptographicException: The system cannot find the file specified: the classic IIS one, and the likely case given "works on my dev box".The PFX is read fine, but by default (
DefaultKeySet, which is usually the user key set) Windows imports the private key into the current user's key store (X509KeyStorageFlags). An app pool identity often has no lo…