I was simply attempting to post a form from one page to another when i encountered the following error:
HttpException (0x80004005): 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. AutoGenerate cannot be used in a cluster.
It seems when developing with .NET 2.0 or later there is a bug in the framework which can become apparent if the following conditions are true:
-
You are not using a web farm or cluster server environment.
-
You are using built-in databound controls such as GridView, DetailsView or FormView which utilize “DataKeyNames”.
-
I even encountered the error when posting to a new page with no code at all!
If these conditions are true and you click a postbacking control/link and the page hasn't loaded completely in client browser, you might get the "Validation of ViewState MAC failed" exception. When this happens, you have 2 choices to resolve the issue:
-
You can just try setting the page property "EnableViewStateMac" to false this should alleviate the problem, although not ideal.
-
It is RECOMMENDED to upgrade to .NET 3.5 SP1 (.NET 2.0 SP2) as it has a fix in it that should resolve this issue for good.
Viewstate MAC
The viewstate mac property indicates whether or not to verify message authentication codes (MAC) in the page's view state when the page is posted back from the client. Generally this property should be true when passing sensitive information between postbacks, or where there is a high risk of tampering. A view state MAC is an encoded version of the hidden variable that a page's view state is persisted to when sent to the browser. When you set the EnableViewStateMac attribute to true, the encoded and encrypted view state is checked to verify that it has not been tampered with on the client. The default value is false.