Deployment slots on azure and authentication

When using the dotnet identity framework the issue of users getting logged off when deploying and switching deployment slots arise. Here comes a solution to that problem.

Before using deployment slots, you're essentially running a single web app and azure provides a default configuration which just makes it work. It's using the DPAPI which is never meant to be used outside of the machine (MSDN Data protection, encryption at rest). However, when adding deployment slots, you're essentially adding another web app which uses a different data protection key.

To solve this, you have to configure the DataProtection provider that is used behind the scenes. You can configure this in multiple ways, using for instance certificates or azure data vault keys. The simplest way is probably to use certificates. You could use your https certificate for this, but remember to add the appsettings "WEBSITE_LOAD_CERTIFICATES" with the thumbnail. See the instructions if you are unsure of how:

Aside from that, you also have to store the data (generated keys) somehow. There are some possibillities there as well, but since the idea is to remove the statefulness on the server, it's probably a good idea to use blob storage or similar instead of local storage, even though that could work if it's mapped between the servers. The data that is stored is just XML and it's fairly trivial to roll your own storage provider (IXmlRepository interface, GetAllElements() and StoreElements(XElement element, string friendlyName)).

To setup dataprotection using a certificate and blob storage, se the code below. The SSL certificate (https) will suffice, thought the behaviour if the certificate is "rekeyed" is yet unknown for me.

.PersistKeysToAzureBlobStorage(cloudStorageAccount, "containerName/keys.xml");

And voial√°, deployment without users getting logged off!

It should also be mentioned that if the code above is used, the container [containerName] will have to be created before usage.

The XML created from this looks like this when created with Microsoft.NetCore.DataProtection, version v2.1.1:

<?xml version="1.0" encoding="utf-8"?>
<key id="ca70b23c-bfd3-4d5c-a43e-ff18aa157bcf" version="1">
<descriptor deserializerType="Microsoft.AspNetCore.DataProtection.AuthenticatedEncryption.ConfigurationModel.AuthenticatedEncryptorDescriptorDeserializer, Microsoft.AspNetCore.DataProtection, Version=, Culture=neutral, PublicKeyToken=adb9793829ddae60">
<encryption algorithm="AES_256_CBC" />
<validation algorithm="HMACSHA256" />
<encryptedSecret decryptorType="Microsoft.AspNetCore.DataProtection.XmlEncryption.EncryptedXmlDecryptor, Microsoft.AspNetCore.DataProtection, Version=, Culture=neutral, PublicKeyToken=adb9793829ddae60"
<EncryptedData Type=""
<EncryptionMethod Algorithm="" />
<KeyInfo xmlns="">
<EncryptedKey xmlns="">
<EncryptionMethod Algorithm="" />
<KeyInfo xmlns="">