使用ACS扩展Azure Web角色时的保护API异常

时间:2012-12-19 12:30:51

标签: asp.net-mvc azure azure-web-roles acs

我一直在使用针对Windows Live和Google的Azure ACS,它一直在运行,没有任何问题。昨晚我们将实例从1个运行实例扩展到3个,从那以后人们在访问我们的网站时报告了问题。我们已将此跟踪到以下异常情况,该异常情况经常发生。

我们假设我们的配置中存在某个问题但不确定我们缺少什么。我们设置机器密钥......

<machineKey decryption="AES" decryptionKey="F7_SOMETHING_SOMETHING_FA" validation="SHA1" validationKey="63_SOMETHING_SOMETHING_BF" />

有人能解释一下这个问题吗?

System.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.  ---> System.Security.Cryptography.CryptographicException: Key not valid for use in specified state.

   at System.Security.Cryptography.ProtectedData.Unprotect(Byte[] encryptedData, Byte[] optionalEntropy, DataProtectionScope scope)
   at Microsoft.IdentityModel.Web.ProtectedDataCookieTransform.Decode(Byte[] encoded)
   --- End of inner exception stack trace ---
   at Microsoft.IdentityModel.Web.ProtectedDataCookieTransform.Decode(Byte[] encoded)
   at Microsoft.IdentityModel.Tokens.SessionSecurityTokenHandler.ApplyTransforms(Byte[] cookie, Boolean outbound)
   at Microsoft.IdentityModel.Tokens.SessionSecurityTokenHandler.ReadToken(XmlReader reader, SecurityTokenResolver tokenResolver)
   at Microsoft.IdentityModel.Tokens.SessionSecurityTokenHandler.ReadToken(Byte[] token, SecurityTokenResolver tokenResolver)
   at Microsoft.IdentityModel.Web.SessionAuthenticationModule.ReadSessionTokenFromCookie(Byte[] sessionCookie)
   at Microsoft.IdentityModel.Web.SessionAuthenticationModule.TryReadSessionTokenFromCookie(SessionSecurityToken& sessionToken)
   at Microsoft.IdentityModel.Web.SessionAuthenticationModule.OnAuthenticateRequest(Object sender, EventArgs eventArgs)
   at System.Web.HttpApplication.SyncEventExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()
   at System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously)

注意:对于上下文。这是在Windows Azure Web角色中运行的,并且是MVC 4。

3 个答案:

答案 0 :(得分:7)

现在您已经扩展,并且由于您的应用程序托管在负载均衡器后面,因此您的用户可能导航到服务器A,在服务器A上获得了受DPAPI保护的会话cookie,但是当他们继续浏览时在站点周围,负载均衡器重定向请求以在服务器B上执行。发生这种情况时,服务器B没有匹配的机器密钥,因此它无法解密会话cookie并抛出上述错误。您可以通过以下三种方式解决此问题。

Windows Identity Foundation(WIF)是必须安装在计算机上的带外运行时,以便您的声明感知应用程序可以使用它。默认情况下,Windows Azure实例上未安装WIF。要运行云声明感知应用程序,必须在Windows Azure实例上提供WIF运行时。最简单的方法是将WIF程序集包含在部署包中。

将WIF程序集包含在Windows Azure部署程序包中

  1. 在解决方案资源管理器中,找到声明感知应用程序。
  2. 展开References文件夹。
  3. 在References文件夹下找到Microsoft.IdentityModel程序集。
  4. 右键单击程序集,然后单击“属性”。
  5. 在属性窗口中,指定将本地复制为True,将特定版本指定为False。
  6. 默认情况下,WIF使用数据保护应用程序编程接口(DPAPI)以加密方式保护cookie。 Windows Azure上不提供DPAPI。要确保您的云声明感知Web应用程序在部署到Windows Azure时能够正常运行,您必须使用RSA添加Cookie加密功能。

    使用RSA加密Cookie

    1. 在解决方案资源管理器中,找到您的云声明感知Web应用程序。
    2. 在Visual Studio编辑器中打开global.asax.cs文件,该文件是global.asax文件背后的代码。
    3. 添加以下声明:

      using Microsoft.IdentityModel.Tokens;
      using Microsoft.IdentityModel.Web;
      using Microsoft.IdentityModel.Web.Configuration;
      

      添加以下代码:

      void OnServiceConfigurationCreated(object sender, ServiceConfigurationCreatedEventArgs e)
      {
          //
          // Use the <serviceCertificate> to protect the cookies that are
          // sent to the client.
          //
          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());
          e.ServiceConfiguration.SecurityTokenHandlers.AddOrReplace(sessionHandler);
      }
      
      void Application_Start(object sender, EventArgs e)
      {
          FederatedAuthentication.ServiceConfigurationCreated += OnServiceConfigurationCreated;
      

      可在此处找到更多信息:http://msdn.microsoft.com/en-us/library/hh289318.aspx

答案 1 :(得分:2)

farm环境中使用WIF时,这是常见的例外情况。关键是默认行为是使用DPAPI加密cookie。但是DPAPI是MachineKey绑定的。

您必须在Global.Asax中进行一些小改动,并使用RSA Crypto服务提供程序来加密/解密FedAuth Cookie。看看this article on how to achieve that

答案 2 :(得分:1)

我尝试使用部署为Windows Azure网站实例和云服务的MVC 4应用程序时遇到类似问题。以下帮助我解决了这个问题。 http://msdn.microsoft.com/en-us/library/hh568644.aspx

滚动到最底部,找到显示删除SessionSecurityTokenHandler并将其替换为MachineKeySessionSecurityTokenHandler的示例。

相关问题