ViewState検証での例外


説明

この記事では、Sitecoreログ ファイルに出力される可能性のある、ViewState認証に関連する例外の最も一般的なシナリオおよび対処方法のヒントについて説明します。

なお、これらの例外は、特にSitecoreにのみ関連するものではなく、すべてのASP.NETアプリケーションに該当するものであることにご留意ください。

シナリオ1

メッセージ:「The state information is invalid for this page and might be corrupted(このページの状態情報は無効です。破損している可能性があります。)」

エラーの例は、次の通りです:

Exception: System.Web.HttpException
Message: The state information is invalid for this page and might be corrupted.
Source: System.Web
   at System.Web.UI.ViewStateException.ThrowError(Exception inner, String persistedState, String errorPageMessage, Boolean macValidationError)
   at System.Web.UI.HiddenFieldPageStatePersister.Load()
   at System.Web.UI.Page.LoadPageStateFromPersistenceMedium()
   at System.Web.UI.Page.LoadAllState()
   at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
   at System.Web.UI.Page.ProcessRequest(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
   at System.Web.UI.Page.ProcessRequest()
   at System.Web.UI.Page.ProcessRequest(HttpContext context)
   at System.Web.HttpApplication.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()
   at System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously)

Nested Exception

シナリオ2

メッセージ:「Validation of viewstate MAC failed. If this application is hosted by a Web Farm or cluster, ensure that <machineKey> configuration specifies the same validationKey and validation algorithm. AutoGenerate cannot be used in a cluster. (viewstate MAC の検証フィールドです。このアプリケーションが Web Farm またはクラスターによってホストされている場合、<machineKey> 構成が同一の validationKey および検証アルゴリズムを指定していることを確認してください。AutoGenerate をクラスターで使用することはできません。)」

エラーの例は、次の通りです:

Exception: System.Web.HttpException
Message: Validation of viewstate MAC failed. If this application is hosted by a Web Farm or cluster, ensure that <machineKey> configuration specifies the same validationKey and validation algorithm. AutoGenerate cannot be used in a cluster.
Source: System.Web
   at System.Web.UI.ViewStateException.ThrowMacValidationError(Exception inner, String persistedState)
   at System.Web.UI.ObjectStateFormatter.Deserialize(String inputString, Purpose purpose)
   at System.Web.UI.Util.DeserializeWithAssert(IStateFormatter2 formatter, String serializedState, Purpose purpose)
   at System.Web.UI.HiddenFieldPageStatePersister.Load()
   at System.Web.UI.Page.LoadPageStateFromPersistenceMedium()
   at System.Web.UI.Page.LoadAllState()
   at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
   at System.Web.UI.Page.ProcessRequest(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
   at System.Web.UI.Page.ProcessRequest()
   at System.Web.UI.Page.ProcessRequest(HttpContext context)
   at System.Web.HttpApplication.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()
   at System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously)

Nested Exception

シナリオ3

メッセージ:「The client disconnected. (クライアントが切断されました。)」

エラーの例は、次の通りです:

Message: The client disconnected.
Source: System.Web
   at System.Web.UI.ViewStateException.ThrowViewStateError(Exception inner, String persistedState)
   at System.Web.UI.ClientScriptManager.EnsureEventValidationFieldLoaded()
   at System.Web.UI.ClientScriptManager.ValidateEvent(String uniqueId, String argument)
   at System.Web.UI.WebControls.TextBox.LoadPostData(String postDataKey, NameValueCollection postCollection)
   at System.Web.UI.Page.ProcessPostData(NameValueCollection postData, Boolean fBeforeLoad)
   at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
   at System.Web.UI.Page.ProcessRequest(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
   at System.Web.UI.Page.ProcessRequest()
   at System.Web.UI.Page.ProcessRequest(HttpContext context)
   at System.Web.HttpApplication.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()
   at System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously)

Nested Exception

解決先

本問題を解決するには、次のオプションを検討し、実施してください:

  1. MAC認証が有効化されている場合、machineKeyが静的に設定されていることを確認してください。そうでない場合、ユーザーがウェブ サイトを閲覧する時に、(例えば、IIS再起動後またはアプリケーション プールのリサイクル後などで)machineKeyが生成された場合、エラーが発生する恐れがあります。machineKeyが変更された後、次のポストバックでMAC認証は失敗します。
  2. MAC認証が有効化された複数インスタンスの環境では、関連するすべてのインスタンスで、machineKeys、「validation」および「decryption」属性が同じであることを確認してください。 
  3. ViewStateをチャンク(複数の非表示フィールド)に分割するように、web.configファイルのページ セクションのMaxPageStateFieldLength属性を設定します。ViewStateのサイズが巨大な場合、一部のブラウザ(通常、モバイル デバイスのブラウザ)では 、非表示フィールドがある値を超えると切り捨てられるため、ViewState検証が失敗する可能性があります。また、ある種のアンチウィルス ソフトウェア・ルータ・プロキシなどが、非常に長い値を持つ非表示フィールドを切り捨てる可能性があります。
  4. 3.5 .NETフレームワークSP1以上がインストールされていることをご確認ください。これは、「DataKeyNames」を利用するGridView、DetailsViewまたはFormViewなどのコントロールを使用する場合に該当します。この特定のシナリオに関する追加情報については、以下のリンクにアクセスしてください:
    https://social.msdn.microsoft.com/Forums/en-US/7464f5d3-e682-445f-9a36-96bcdfade6e9/validation-of-viewstate-mac-failed-the-state-information-is-invalid-for-this-page-and-might-be?forum=aspstatemanagement
  5. ViewState認証が失敗し、サーバで要求の処理が完了する前にユーザー・ブラウザが何らかの原因で切断されると、シナリオ3が発生する可能性があります。
    解決策1~4をを適用することで、上述の例外がログ ファイルに出力される可能性を減らすことができます。