Sunday, August 5, 2012

Free eBooks

Microsoft has been kind enough to release a number of free eBooks around their technology, and finally a number of them in a format besides PDF. If you want to read up one things like Azure, Windows Phone, SharePoint and Office here are some good starting points.

Azure, SharePoint, WP, Windows 8, Office – set 1

Azure, SharePoint, WP, Windows, Office – set 2

Friday, August 3, 2012

Running WIF Relying parties in Windows Azure

I am coping this blog from another blog here. Copying it here just to make sure I can find it in the future. My Azure app had this issue and this fixed the problem. 

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:

next upload the certificate to the hosted service and declare it in the LocalMachine certificate store of the running role.

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[] 
          new DeflateCookieTransform(), 
          new RsaEncryptionCookieTransform(e.ServiceConfiguration.ServiceCertificate),
          new RsaSignatureCookieTransform(e.ServiceConfiguration.ServiceCertificate)
       SessionSecurityTokenHandler sessionHandler =
         new SessionSecurityTokenHandler(sessionTransforms.AsReadOnly());




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.