ASP.NET 2.0 and "Validation of ViewState Mac failed" exception
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 loads 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 get 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!
-
<
EDIT: I also sent some feedback on the MS's product feedback system: http://lab.msdn.microsoft.com/ProductFeedback/viewfeedback.aspx?feedbackid=e08714b5-35cc-4520-9876-fe2851018453
# re: ASP.NET 2.0 and "Validation of ViewState Mac failed" exception
it's a very good description of the problem we've "solved" together - or better - you've solved :-)
keep up the good work,
cheerio
crystal.net
# re: ASP.NET 2.0 and "Validation of ViewState Mac failed" exception
The page in question has about 90 user controls (textboxes and checkboxes) and some serious javascript / c# code in place. It contains one formview with the DataKeyNames value set. The page load time is around a full second between sending it to the browser, and the browsers startup script doing the appropriate wizardry.
None of our other pages exhibited a problem with the webfarm.
I implemented the fix you suggested and it works perfectly. So, I just wanted to drop a note that this does happen in a webfarm scenario as well.
Thanks!
# re: ASP.NET 2.0 and "Validation of ViewState Mac failed" exception
interesting though of course slow loading Page applies in web farm too. It's just that it's more often the validation key setting that's missing from machines in web farm scenario.
# re: ASP.NET 2.0 and "Validation of ViewState Mac failed" exception
I had a master page where I (mistakenly, as I discovered later) had added a nested form inside the form tag that's already there by default. This generated no errors.
But when I created a page based on this master page, and added a FormView, I got the error when I tried to edit a record. It went away when I modified the web.config file as suggested above, but editing the formview data always failed (it cleared all the fields).
As soon as I removed the nested form in the master page, everything was OK.
Jos
# re: ASP.NET 2.0 and "Validation of ViewState Mac failed" exception
# re: ASP.NET 2.0 and "Validation of ViewState Mac failed" exception
Note that with ASP.NET 1.x there was no event validation or viewstate encryption so what you miss is the added security in v2, if you use these workarounds (which were meant to the case when the issue occurs with one of the databound controls, and when you are sure your host etc is not using a web farm).
If you use SSL for the web site, it basically is a moot point, though not of course removing the need of these for sites not using SSL.
The one you get on www.asp.net is most probably indeed related to the web farm as the site uses such, as far as I know. You should use the site's Feedback forum: http://forums.asp.net/188/ShowForum.aspx
I can also ping someone on Telligent so that they are aware of it.
# re: ASP.NET 2.0 and "Validation of ViewState Mac failed" exception
I was using GridView control, When i was using paging I was getting same error message, However I used your solution and It works very nice.
Thanks again.
Raj Gohil
# re: ASP.NET 2.0 and "Validation of ViewState Mac failed" exception
I got the solution of this problem.
The cause of this problem is multiple form tag in pages.
there should be only one pair of form tag in page.
Mahesh
singh.mahesh@gmail.com
www.o3developers.com
# 验证视图状态 MAC 失败。如果此应用程序由网络场或群集承载,请确保 配置指定了相同的 validationKey 和验证算法。不能在群集中使用 AutoGenerate。
# re: ASP.NET 2.0 and "Validation of ViewState Mac failed" exception
# re: ASP.NET 2.0 and "Validation of ViewState Mac failed" exception
http://www.andreas-kraus.net/blog/validation-of-viewstate-mac-failed/
It might be the solution until it's officially fixed.
# re: ASP.NET 2.0 and "Validation of ViewState Mac failed" exception
did not solve my problem, but
<pages enableEventValidation="false" viewStateEncryptionMode="Never" enableViewStateMac="false"/>
did ;)
# re: ASP.NET 2.0 and "Validation of ViewState Mac failed" exception
# re: ASP.NET 2.0 and "Validation of ViewState Mac failed" exception
<pages enableEventValidation="false" viewStateEncryptionMode="Never" enableViewStateMac="false"/>
# re: ASP.NET 2.0 and "Validation of ViewState Mac failed" exception
# 网站测试中出现 验证视图MAC失败
验证视图MAC失败。如果此引用程序由网络场或群集承载,请确保&lt;machineKey&gt;配置指定了相同的 validationKey 和验证算法。不能在群集中使用...
# re: ASP.NET 2.0 and "Validation of ViewState Mac failed" exception
# Validation of viewstate MAC failed