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



Will you give this article a +1 ? Thanks in advance




About The Author

Suprotim Agarwal, ASP.NET Architecture MVP works as an Architect Consultant and provides consultancy on how to design and develop Web applications.

Suprotim is also the founder and primary contributor to DevCurry, DotNetCurry and SQLServerCurry. He has also written an EBook 51 Recipes using jQuery with ASP.NET Controls.

Follow him on twitter @suprotimagarwal

comments

2 Responses to "Resolving the error “Validation of viewstate MAC failed” in an ASP.NET 3.5 application"
  1. Geoff said...
    February 7, 2011 at 8:49 AM

    Sorry if this is obvious but...

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

    Thanks.

  2. Suprotim Agarwal said...
    February 8, 2011 at 8:29 PM

    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.

 

Copyright © 2009-2014 All Rights Reserved for DevCurry.com by Suprotim Agarwal | Terms and Conditions