这实际上是在推动我的香蕉 – 从明天开始我正在度假,但如果我今天无法工作那么它就会受到威胁 – 所以任何帮助都非常感激!
首先,我的网站有一个手动定义的< machinekey />已定义元素,以便Web场中的两个Web服务器保持同步.我已经通过IIS管理器对此进行了验证(因此,尽管与其他许多问题相似,我仍然会问.)它看起来像这样(键被省略 – 但它们是正确的长度):
<machineKey validationKey="[512 bit hex]" decryptionKey="[256-bit hex]" validation="SHA1" decryption="AES" />
该网站正在运行Asp.Net MVC3,我在’正常’模式下使用Forms身份验证(即不是2.0 compat模式).我正在使用表单身份验证来创建身份验证票证 – 使用如下代码:
FormsAuthentication.SetAuthCookie(userName,false);
My Forms Auth配置非常简单;没有IIS或服务器范围的设置覆盖记录的默认值:
<authentication mode="Forms"> <forms defaultUrl="~/Unauthorised" loginUrl="~/Unauthorised" ticketCompatibilityMode="Framework40" /> </authentication>
然后我劫持了cookie读取功能as per this MSDN topic,这样我就可以创建我想要的主体和身份.
问题是 – 只有一半的Web服务器场能够解密身份验证cookie,另一半(即任何一个未对用户进行身份验证)只能给出:
System.Security.Cryptography.CryptographicException:要解密的数据长度无效.
将此作为堆栈跟踪的顶部:
[CryptographicException: Length of the data to decrypt is invalid.] System.Security.Cryptography.RijndaelManagedTransform.TransformFinalBlock(Byte[] inputBuffer,Int32 inputOffset,Int32 inputCount) +12521039 System.Security.Cryptography.CryptoStream.FlushFinalBlock() +53 System.Web.Configuration.MachineKeySection.EncryptOrDecryptData(Boolean fEncrypt,Byte[] buf,Byte[] modifier,Int32 start,Int32 length,Boolean useValidationSymAlgo,Boolean useLegacyMode,IVType ivType) +331 System.Web.Security.FormsAuthentication.Decrypt(String encryptedTicket) +293
我们使用的是一个稍微古老(但非常可靠)的负载均衡器 – 它不会修改HTTP流量 – 这种情况下的流量是HTTP;我能够验证它始终是一台服务器,而不是通过使用fiddler来解密和检查流量.
这显然指出了不同步的机器 – 但它们不是 – 所以不知道发生了什么!!
提前致谢!
解决方法
当不同的服务器具有不同的补丁时,它有可能改变加密(/ decryption)的行为,因此可能导致此运行时异常:
System.Security.Cryptography.CryptographicException: Length of the data to decrypt is invalid.
有关此问题的更多信息,请访问:http://blog.evonet.com.au/post/SystemSecurityCryptographyCryptographicException-Length-of-the-data-to-decrypt-is-invalid.aspx