AspNetCore.Docs icon indicating copy to clipboard operation
AspNetCore.Docs copied to clipboard

Is any ProtectKeysWith* supported in Linux containers on-premise?

Open jesperkristensen opened this issue 2 years ago • 2 comments
trafficstars

I am trying to set up ASP.NET Core apps in Linux containers in an on-premises setup, so it does not print this warning: No XML encryptor configured. Key {...} may be persisted to storage in unencrypted form.

Of the built-in options, ProtectKeysWithAzureKeyVault is not applicable for us since we don't have access to Azure, and ProtectKeysWithDpapi and ProtectKeysWithDpapiNG are not applicable because we run in Linux containers. ProtectKeysWithCertificate takes either a certificate or thumbprint as input, but the documentation has no guidelines about what types of certificate are appropriate.

Can you add an example of how to create and use a certificate that is appropriate to use with Data Protection?

Is there a dotnet CLI command to generate such a certificate?


Document Details

Do not edit this section. It is required for learn.microsoft.com ➟ GitHub issue linking.

jesperkristensen avatar Jun 05 '23 10:06 jesperkristensen

Just had to do this myself. You can use ProtectKeysWithCertificate with a self-signed certificate created via openssl or dotnet dev-certs (or any other tool that can create an X.509 certificate). Make sure it is in the .pfx format, and on Linux it is easiest to just directly instantiate the X509Certificate2 with the path to the .pfx file and the password, then pass that certificate into ProtectKeysWithCertificate.

var redis = ConnectionMultiplexer.Connect(Configuration["Redis"]);
var cert = new X509Certificate2(Configuration["CertificatePath"], Configuration["CertificatePassword"]);
services.AddDataProtection()
    .SetApplicationName("MyApplication")
    .ProtectKeysWithCertificate(cert)
    .PersistKeysToStackExchangeRedis(redis, "DataProtection-Keys");

But yes I agree the docs could be improved here.

stevesw avatar Dec 02 '23 06:12 stevesw