在多服务器环境中,当会话过期并且应用程序的另一个实例使用相同的会话ID和计算机密钥,但位于不同服务器上时,可能会发生此错误。起初,每个服务器都会生成自己的机器密钥,后者与应用程序的单个实例相关联。当会话过期并且当前服务器繁忙时,应用程序将通过负载均衡器重定向到更具操作性的服务器。以我为例,我从多台服务器上运行相同的应用程序,错误消息:
Validation of viewstate MAC failed. If this application is hosted by a Web Farm or cluster, ensure that configuration specifies the same validationKey and validation algorithm
下在web.config中定义的机器代码有解决的问题。基于微软的解决方案1A ,https://support.microsoft.com/en-us/kb/2915218#AppendixA
# Generates a <machineKey> element that can be copied + pasted into a Web.config file.
function Generate-MachineKey {
[CmdletBinding()]
param (
[ValidateSet("AES", "DES", "3DES")]
[string]$decryptionAlgorithm = 'AES',
[ValidateSet("MD5", "SHA1", "HMACSHA256", "HMACSHA384", "HMACSHA512")]
[string]$validationAlgorithm = 'HMACSHA256'
)
process {
function BinaryToHex {
[CmdLetBinding()]
param($bytes)
process {
$builder = new-object System.Text.StringBuilder
foreach ($b in $bytes) {
$builder = $builder.AppendFormat([System.Globalization.CultureInfo]::InvariantCulture, "{0:X2}", $b)
}
$builder
}
}
switch ($decryptionAlgorithm) {
"AES" { $decryptionObject = new-object System.Security.Cryptography.AesCryptoServiceProvider }
"DES" { $decryptionObject = new-object System.Security.Cryptography.DESCryptoServiceProvider }
"3DES" { $decryptionObject = new-object System.Security.Cryptography.TripleDESCryptoServiceProvider }
}
$decryptionObject.GenerateKey()
$decryptionKey = BinaryToHex($decryptionObject.Key)
$decryptionObject.Dispose()
switch ($validationAlgorithm) {
"MD5" { $validationObject = new-object System.Security.Cryptography.HMACMD5 }
"SHA1" { $validationObject = new-object System.Security.Cryptography.HMACSHA1 }
"HMACSHA256" { $validationObject = new-object System.Security.Cryptography.HMACSHA256 }
"HMACSHA385" { $validationObject = new-object System.Security.Cryptography.HMACSHA384 }
"HMACSHA512" { $validationObject = new-object System.Security.Cryptography.HMACSHA512 }
}
$validationKey = BinaryToHex($validationObject.Key)
$validationObject.Dispose()
[string]::Format([System.Globalization.CultureInfo]::InvariantCulture,
"<machineKey decryption=`"{0}`" decryptionKey=`"{1}`" validation=`"{2}`" validationKey=`"{3}`" />",
$decryptionAlgorithm.ToUpperInvariant(), $decryptionKey,
$validationAlgorithm.ToUpperInvariant(), $validationKey)
}
}
然后: ,但使用的第三方网站生成代码可能已损坏,请从您的命令shell中运行这个
对于ASP.NET 4.0
Generate-MachineKey
您的密钥会看起来像:<machineKey decryption="AES" decryptionKey="..." validation="HMACSHA256" validationKey="..." />
对于ASP.NET 2.0和3。5
Generate-MachineKey -validation sha1
您的密钥会看起来像:<machineKey decryption="AES" decryptionKey="..." validation="SHA1" validationKey="..." />
一些更多的信息是需要。这是在你的单一开发者机器上吗?生产服务器?是否有多个生产服务器(即网络农场)? – 2011-04-30 09:49:01
我的开发团队中有同样的错误。有些电脑出现这个错误,有些电脑却没有。任何人都可以提供答案? (这两个贴都不能解决我的问题) – Apolo 2015-04-15 15:16:58
我在MSDN上看到这篇文章(http://blogs.msdn.com/b/webtopics/archive/2009/05/13/viewstate-exception-may-come-with -action-attribute-in-form-tag-post-net-framework-3-5-sp1.aspx),你有代码来重现。提供的解决方案(删除表单的动作属性)并不好(因为我的表单将无处可去) – Apolo 2015-04-15 15:18:30