マルチサーバー環境では、このエラーは、セッションの有効期限が切れ、アプリケーションの別のインスタンスが同じセッション ID とマシン キーを使用して別のサーバーに再起動された場合に発生する可能性があります。最初に、各サーバーが独自のマシン キーを生成します。このマシン キーは、後でアプリケーションの 1 つのインスタンスに関連付けられます。セッションが期限切れになり、現在のサーバーがビジー状態になると、アプリケーションは、ロード バランサーを介してより稼働しているサーバーにリダイレクトされます。私の場合、複数のサーバーから同じアプリを実行すると、次のエラー メッセージが表示されます。
ビューステート MAC の検証に失敗しました。このアプリケーションが Web ファームまたはクラスターによってホストされている場合は、構成で同じ validationKey と検証アルゴリズムが指定されていることを確認してください。
web.config でマシンコードを定義すると、問題が解決しました。ただし、破損する可能性のあるコード生成にサード パーティのサイトを使用する代わりに、コマンド シェルからこれを実行してください。
# 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)
}
}
それで:
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="..." />