2014年10月24日金曜日

Viewstate 検証に失敗しました。 原因: 指定された viewstate で整合性チェックに失敗しました。

ASP.NET Forms で顧客 db を開発・運用しています。
しかしたまに例の黄色いエラー画面が出ます。

そういう場合は、サーバーマシンのイベントビューアを確認します。 内容は↓参照

(2016/6/11追記)
どうも、machineKey という物が有り、定期的に自動生成されるみたいです。
これが viewstate 無効化の原因になるようです。
参考: ASP.NET と MachineKey の関係をまとめてみた

うちではこんな感じで Web.config に対策するようにしています:
 
<configuration>
 <system.web>
  <machineKey validationKey="0ED6BCBA637BA0EC06E3FACD3925B6087731147597AB33EF7A033FF047508878FA67819F24AE6C8624A71E16A26A22A72145C478C1527D35AE7A8685D8EB5E82"
              decryptionKey="A44271A23EE2CC673476A17222DE0D86AF104EBEFC3A97CA"
              validation="SHA1"/>
 </system.web>
</configuration>

ジェネレータはこのような感じです:

using System;
using System.Text;
using System.Security.Cryptography;

namespace Crypto {
    public class KeyCreator {
        public static void Main(String[] args) {
            String[] commandLineArgs = System.Environment.GetCommandLineArgs();
            if (commandLineArgs.Length < 3) {
                Console.Error.WriteLine("machineKeyGenerator 24 64");
                Environment.ExitCode = 1;
                return;
            }
            string decryptionKey = CreateKey(System.Convert.ToInt32(commandLineArgs[1]));
            string validationKey = CreateKey(System.Convert.ToInt32(commandLineArgs[2]));
            Console.WriteLine("<configuration>");
            Console.WriteLine(" <system.web>");
            Console.WriteLine("  <machineKey validationKey=\"{0}\"", validationKey);
            Console.WriteLine("              decryptionKey=\"{0}\"", decryptionKey);
            Console.WriteLine("              validation=\"SHA1\"/>");
            Console.WriteLine(" </system.web>");
            Console.WriteLine("</configuration>");

            Console.ReadLine();
        }
        static String CreateKey(int numBytes) {
            RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider();
            byte[] buff = new byte[numBytes];
            rng.GetBytes(buff);
            return BytesToHexString(buff);
        }
        static String BytesToHexString(byte[] bytes) {
            StringBuilder hexString = new StringBuilder(64);
            for (int counter = 0; counter < bytes.Length; counter++) {
                hexString.Append(String.Format("{0:X2}", bytes[counter]));
            }
            return hexString.ToString();
        }
    }
}

GAE 上にジェネレーターを用意しました。精度が気にならないのであればこちらからでも。

ーーー
Event code: 4009
Event message: Viewstate 検証に失敗しました。 原因: 指定された viewstate で整合性チェックに失敗しました。
Event time: 2014/10/24 9:15:52
Event time (UTC): 2014/10/24 0:15:52
Event ID: 86a80a707a2d41b8be290a342d793e29
Event sequence: 98
Event occurrence: 1
Event detail code: 50203

Application information:
    Application domain: /LM/W3SVC/2/ROOT/hdb-3-130585818700385545
    Trust level: Full
    Application Virtual Path: 秘密
    Application Path: 秘密
    Machine name: DD7

Process information:
    Process ID: 6832
    Process name: w3wp.exe
    Account name: IIS APPPOOL\.NET v2.0

Request information:
    Request URL: 秘密
    Request path: 秘密
    User host address: 秘密
    User: 
    Is authenticated: False
    Authentication Type: 
    Thread account name: IIS APPPOOL\.NET v2.0

ViewStateException information:
    Exception message: 無効な viewstate です。
    Client IP: 秘密
    Port: 46470
    User-Agent: Mozilla/5.0 (Linux; U; Android 4.1.1; ja-jp; ME172V Build/JRO03H) AppleWebKit/534.30 (KHTML, like Gecko) Version/4.0 Safari/534.30
    PersistedState: 秘密
    Referer: 秘密
    Path: 秘密

Custom event details:
ーーー

0 件のコメント:

コメントを投稿