Friday, July 23, 2010

ASP.NET Viewstate MAC failed Error ... What is this?

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




4 comments: