Resolving the error “Validation of viewstate MAC failed” in an ASP.NET 3.5 application

Have you encountered the error

“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.”

in your ASP.NET application while clicking a button?

The reason for this error is ASP.NET renders some hidden fields, such as the __EVENTVALIDATION field near the bottom of the form, immediately before the closing </div> tag. If event validation is enabled and a user clicks on a button before the page is rendered completely, then __EVENTVALIDATION field is not included in the data that is being posted. Therefore, the .NET Framework treats this postback as invalid and brings up this error.

To resolve this error, a lot of developers set the page property "EnableViewStateMac" to false. Although this may resolve the error in some cases, this is not the preferred way.

To resolve this error, if you are using .NET 3.5, upgrade to .NET 3.5 SP1. If you are using .NET 2.0, then upgrade to .NET 2.0 SP2.

By default, in ASP.NET 3.5 SP1, all system-generated hidden fields are rendered at the top of the page. This makes sure that the information in these fields is sent to the server even if a postback is performed, before the page has finished loading.

If you want to render system-generated fields at the bottom of the page, set the RenderAllHiddenFieldsAtTopOfForm property to False.

<pages renderAllHiddenFieldsAtTopOfForm="false" />


Here is one good resource to learn more about this error






About The Author

Suprotim Agarwal
Suprotim Agarwal, Developer Technologies MVP (Microsoft Most Valuable Professional) is the founder and contributor for DevCurry, DotNetCurry and SQLServerCurry. He is the Chief Editor of a Developer Magazine called DNC Magazine. He has also authored two Books - 51 Recipes using jQuery with ASP.NET Controls. and The Absolutely Awesome jQuery CookBook.

Follow him on twitter @suprotimagarwal.

3 comments:

TheRoboHawk said...

Sorry if this is obvious but...

Are you saying that the .NET RUNTIME needs to be upgraded? On the hosting Server?

Thanks.

Suprotim Agarwal said...

Geoff. The service pack contains the fixes. So yes you have to install these packs on your server. To resolve this error, if you are using .NET 3.5, upgrade to .NET 3.5 SP1. If you are using .NET 2.0, then upgrade to .NET 2.0 SP2.

Razack said...

I got this error for working on cross site posting with ASP.Net webform in VS 2013. I tried with giving same machine key for both application and set EnableViewStateMac="false" . Is there any solution to fix this error.