ASP.NET 2.0 and "Validation of ViewState Mac failed" exception

Posted by: Jotekes Blog, on 02 Feb 2006 | View original | NEW Bookmarked: 0 time(s)

If you get this Exception

[HttpException (0x80004005): 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.]

and

  • you know *for sure* that you aren't using a web farm
  • it seems that it appears when using built-in databound controls such as GridView, DetailsView or FormView which utilize DataKeyNames.
  • it appears if you have a large page which slows slowly for any reason

If following preconditions are true and you click a postbacking control/link while the Page hasn't loaded completely, you might the "Validation of ViewState MAC failed"  exception. In this case be sure to check following post on ASP.NET Forums where this has been discussed quite thoroughly : http://forums.asp.net/1173230/ShowPost.aspx

It appears because GridView using DataKeyNames requires ViewState to be encrypted. And when ViewState is encrypted, Page adds  <input type="hidden" name="__VIEWSTATEENCRYPTED" id="__VIEWSTATEENCRYPTED" value=" /> field just before closing of the <form> tag. But this hidden field might not bet yet rendered to the browser with long-running pages, and if you make a postback before it "gets down", browser initiates postback without this field (in form post collection)

End result is that if this field is omitted on postback, Page doesn't "know" that viewstate is encrypted and causes the prementioned Exception. E.g Page expects to be fully-loaded before you can make a postback. And by the way similar problem is with event validation since __EVENTVALIDATION field is also rendered on the end of the form.

A way to overcome the problem is to set in web.config

  • <pages enableEventValidation="false" viewStateEncryptionMode ="Never" />

    Just note the security implications of these!

Advertisement
Category: ASP.NET | Other Posts: View all posts by this blogger | Report as irrelevant | View bloggers stats | Views: 11743 | Hits: 462

Similar Posts

  • ASP.NET MVC: DevExpress Mail Demo more
  • Export Word documents to XPS - Open XML Paper Specification format more
  • Bind InfoPath form to XML, Tables, SharePoint Lists & Web Services more
  • Create or Manage XPDL 1.0 & 2.1 packages using Aspose.Workflow more
  • Application Identifiers (AI) for EAN-128 barcode generation more
  • Unit Testing - Do Repeat Yourself more
  • Create charts & add ad hoc capabilities to .NET Web & WinForm apps more
  • Adding Syntactic Sugar to the Spark View Engine more
  • Lookbehind in Regex searches more
  • MaskedInput Transform Intellisense more

News Categories

.NET | Agile | Ajax | Architecture | ASP.NET | BizTalk | C# | Certification | Data | DataGrid | DataSet | Debugger | DotNetNuke | Events | GridView | IIS | Indigo | JavaScript | Mobile | Mono | Patterns and Practices | Performance | Podcast | Refactor | Regex | Security | Sharepoint | Silverlight | Smart Client Applications | Software | SQL | VB.NET | Visual Studio | W3 | WCF | WinFx | WPF | WSE | XAML | XLinq | XML | XSD