Running WIF Relying parties in Windows Azure
When running in a multi server environment like windows azure it is required to make sure the cookies generated by WIF are encrypted with the same pair of keys so all servers can open them.
Encrypt cookies using RSA
In Windows Azure, the default cookie encryption mechanism (which uses DPAPI) is not appropriate because each instance has a different key. This would mean that a cookie created by one web role instance would not be readable by another web role instance. This could lead to service failures effectively causing denial of the service. To solve this problem you should use a cookie encryption mechanism that uses a key shared by all the web role instances. The following code written to global.asax shows how to replace the default SessionSecurityHandler object and configure it to use the RsaEncryptionCookieTransform class:
void Application_Start(object sender, EventArgs e)
FederatedAuthentication.ServiceConfigurationCreated += OnServiceConfigurationCreated;
private void OnServiceConfigurationCreated(object sender, ServiceConfigurationCreatedEventArgs e)
List<CookieTransform> sessionTransforms =
new List<CookieTransform>(new CookieTransform
SessionSecurityTokenHandler sessionHandler =
next upload the certificate to the hosted service and declare it in the LocalMachine certificate store of the running role.
Failing to do the above will generate the following exception when running a relying party in azure: "InvalidOperationException: ID1073: A CryptographicException occurred when attempting to decrypt the cookie using the ProtectedData API". It means that decryption with DPAPI failed. It makes sense because DPAPI key is coupled with the physical machine it is running on.
After changing the encryption policy (like so) make sure to delete all existing cookies other wise you will get the following exception: CryptographicException: ID1014: The signature is not valid. The data may have been tampered with. (It means that an old DPAPI cookie is being processed by the new RSA policy and that will obviously will fail.