Chris Anderson
Chris Anderson

Reputation: 8369

Is there a way to specify a machine key for Azure Websites without specifying it in the web.config?

I want to specify a custom machine key for my websites running on Azure, so I can swap between staging and production and keep the environment consistent between the two without users being "logged out" whenever I do a swap (because otherwise the machine key changes and the user's cookies can't be decrypted anymore). I've previously been setting this in the web.config file, but I don't really like having this value stored in source control (I'm continuously deploying changes to the server). Connection strings can be specified in the Azure portal to avoid this problem. Is there a solution for machine keys?

Upvotes: 3

Views: 3350

Answers (2)

Zain Rizvi
Zain Rizvi

Reputation: 24636

I'm not 100% sure I understand you question correctly, so I'll answer both possible interpretations.

Interpretation #1: Before you swap prod and stage your users got key A when they were accessing the (old) prod. When you do the swap you want users to keep getting key A when they hit the new prod.

Use App Settings. You can set them using the Portal or Powershell. Those are key value strings that you can set and they are accessible as environment variables from your site. When you swap you prod and stage slots the app settings that were on the old prod all move to the new prod, so your customers will see the same values for them.

Interpretation #2: Before you swap prod and stage your users got key A when they were accessing the (old) prod, and key B when the accessed the old staging slot. When you do the swap you want users to getting key B when they hit the new prod and key A when the access the new staging slot

Using sticky settings. Those are app settings that you set for the site but you configure them to stay with the site that they were on, meaning that when you swap sites you swap the settings as well. You can make app settings sticky by using the following powershell command.

Set-AzureWebsite -Name mysite -SlotStickyAppSettingNames @("myslot", "myslot2")

Full details in this link: http://blog.amitapple.com/post/2014/11/azure-websites-slots/#.VMftXHl0waU

Upvotes: 0

Fabrizio Accatino
Fabrizio Accatino

Reputation: 2292

In your web.config reference an external config file for the machinekey section:

  <system.web >
    <machineKey configSource="mkey.config"/>
  </system.web>

Create a file mkey.config like this:

<machineKey 
 validationKey="32E35872597989D14CC1D5D9F5B1E94238D0EE32CF10AA2D2059533DF6035F4F"
 decryptionKey="B179091DBB2389B996A526DE8BCD7ACFDBCAB04EF1D085481C61496F693DF5F4" />

Upload the mkey.config file to Azure web site using ftp instead of web deploy.

Upvotes: 3

Related Questions