I am trying to setup Data Protection in my Azure web apps, so when I swap between staging and production, it doesn't log everyone out. I am stuck on encrypting the keys with a self-signed certificate in Azure.
My question is very much related to: ASP.Net Core Data Protection API in a Clustered Environment However, the answer on this does not work for me as I had already gotten this far.
My code works perfectly fine when I run it locally with a certificate installed to my machince, but when I deploy to my Azure web app, it wont start and just throws a generic error:
Unhandled Exception: System.Security.Cryptography.CryptographicException: Exception of type 'System.Security.Cryptography.CryptographicException' was thrown.
To setup my data protection, I am using the following code in ConfigureServices:
// Add data protection
var storageAccount = CloudStorageAccount.Parse(Configuration["BlobStorage:ConnectionString"]);
var client = storageAccount.CreateCloudBlobClient();
var container = client.GetContainerReference(Configuration["DataProtection:ContainerName"]);
container.CreateIfNotExistsAsync().Wait();
services.AddDataProtection()
.SetApplicationName(Configuration["DataProtection:ApplicationName"])
.PersistKeysToAzureBlobStorage(container, Configuration["DataProtection:BlobName"]);
.ProtectKeysWithDpapiNG($"CERTIFICATE=HashId:{Configuration["Authentication:SingingCertThumbprint"]}",
DpapiNGProtectionDescriptorFlags.None);
The certificate I am using is self-signed and I have uploaded it to Azure through the SSL certificates section on the web app (however, as suggested on other posts, I have also tried a trusted certificate - no luck).
I am using the WEBSITE_LOAD_CERTIFICATES app setting in my Azure web app, so this is not the problem either.
Also worth noting, if I remove
.ProtectKeysWithDpapiNG($"CERTIFICATE=HashId:{Configuration["Authentication:SingingCertThumbprint"]}",
DpapiNGProtectionDescriptorFlags.None);
Then the web app will start and run fine. But now my keys are obviously being stored unencrypted.
Any help would be much appreciated, thanks.