title: SessionSecurityTokenHandler trying to decrypt SessionSecurityToken in RSA-encrypted cookie using DPAPI; why?
body: <p>I have read in MSDN forums, Dominic Baier's blog, and in other sources that DPAPI will not work out-of-the box in Azure, and that one approach to handling federated authentication in any kind of web farm scenario is to replace the DPAPI transforms with one that uses a private key available across the farm, such as RSA encryption using an X509 certificate.  I have taken this approach in my Azure MVC application and configured the <code>SessionSecurityTokenHandler</code> like this:</p>
  
  <pre><code>FederatedAuthentication.ServiceConfigurationCreated += (sender, args) =&gt;
      {
          var sessionTransforms = new List&lt;CookieTransform&gt;(new CookieTransform[]
              {
                  new DeflateCookieTransform(),
                  new RsaEncryptionCookieTransform(args.ServiceConfiguration.ServiceCertificate),
                  new RsaSignatureCookieTransform(args.ServiceConfiguration.ServiceCertificate)
              });
          var sessionHandler = new SessionSecurityTokenHandler(sessionTransforms.AsReadOnly());
          args.ServiceConfiguration.SecurityTokenHandlers.AddOrReplace(sessionHandler);                    
      };
  </code></pre>
  
  <p>Using this configuration, we are able to receive tokens from an identity provider and issue secure cookies encrypted using these transforms.  Running in the Azure emulator, everything works as expected.  However, in the Azure environment, we intermittently see the following error in the browser:</p>
  
  <pre><code>Key not valid for use in specified state.
  
  Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 
  
  Exception Details: System.Security.Cryptography.CryptographicException: Key not valid for use in specified state.
  
  
  Source Error: 
  
  An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.
  
  Stack Trace: 
  
  
  [CryptographicException: Key not valid for use in specified state.
  ]
     System.Security.Cryptography.ProtectedData.Unprotect(Byte[] encryptedData, Byte[] optionalEntropy, DataProtectionScope scope) +577
     Microsoft.IdentityModel.Web.ProtectedDataCookieTransform.Decode(Byte[] encoded) +80
  
  [InvalidOperationException: ID1073: A CryptographicException occurred when attempting to decrypt the cookie using the ProtectedData API (see inner exception for details). If you are using IIS 7.5, this could be due to the loadUserProfile setting on the Application Pool being set to false. ]
     Microsoft.IdentityModel.Web.ProtectedDataCookieTransform.Decode(Byte[] encoded) +433
     Microsoft.IdentityModel.Tokens.SessionSecurityTokenHandler.ApplyTransforms(Byte[] cookie, Boolean outbound) +189
     Microsoft.IdentityModel.Tokens.SessionSecurityTokenHandler.ReadToken(XmlReader reader, SecurityTokenResolver tokenResolver) +862
     Microsoft.IdentityModel.Tokens.SessionSecurityTokenHandler.ReadToken(Byte[] token, SecurityTokenResolver tokenResolver) +109
     Microsoft.IdentityModel.Web.SessionAuthenticationModule.ReadSessionTokenFromCookie(Byte[] sessionCookie) +356
     Microsoft.IdentityModel.Web.SessionAuthenticationModule.TryReadSessionTokenFromCookie(SessionSecurityToken&amp; sessionToken) +123
     Microsoft.IdentityModel.Web.SessionAuthenticationModule.OnAuthenticateRequest(Object sender, EventArgs eventArgs) +61
     System.Web.SyncEventExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +80
     System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean&amp; completedSynchronously) +270
  </code></pre>
  
  <p>This seems to suggest that the <code>SessionSecurityTokenHandler</code> is trying to decrypt the cookie using DPAPI, but why?  Didn't I configure it to use RSA above?</p>