在多服務器環境中,當會話過期並且應用程序的另一個實例使用相同的會話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