Welcome to AspAdvice Sign in | Join | Help

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

Published Thursday, February 02, 2006 5:44 PM by joteke
Filed under:

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Comments

# re: ASP.NET 2.0 and &quot;Validation of ViewState Mac failed&quot; exception

hey joteke,

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
Monday, February 06, 2006 3:47 AM by crystal.net

# re: ASP.NET 2.0 and &quot;Validation of ViewState Mac failed&quot; exception

We ran into this problem yesterday when beta testing an app built in .net 2.0. The only difference between our problem and yours is that we do have a web farm; two '03 servers that are load balanced via the built in network balancing features of win2003.

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!
Monday, February 13, 2006 1:49 PM by Chris Lively

# re: ASP.NET 2.0 and &quot;Validation of ViewState Mac failed&quot; exception

Hi Chris,

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.
Monday, February 13, 2006 2:32 PM by Teemu

# re: ASP.NET 2.0 and &quot;Validation of ViewState Mac failed&quot; exception

I got the same error in a completely different scenario. I'm offering it here so it may help others.

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
Thursday, February 23, 2006 8:16 AM by Jos Branders

# re: ASP.NET 2.0 and &quot;Validation of ViewState Mac failed&quot; exception

The security implications worry me. I get this error when running the Quickstart Trigger application on Microsoft's own site. Seems like there should be a better solution somehow.
Saturday, February 25, 2006 4:41 PM by Peter Kellner

# re: ASP.NET 2.0 and &quot;Validation of ViewState Mac failed&quot; exception

It definately should be fixed with the right solution, e.g so that you wouldn't need to turn anything security related off. That's why I sent the report to MS Feedback system.

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.

Sunday, February 26, 2006 2:18 AM by joteke

# re: ASP.NET 2.0 and &quot;Validation of ViewState Mac failed&quot; exception

Thanks very much.

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
Wednesday, April 12, 2006 3:37 PM by Raj Gohil

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

Hi Guys,
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
Wednesday, May 10, 2006 3:19 AM by Mahesh

# 验证视图状态 MAC 失败。如果此应用程序由网络场或群集承载,请确保 配置指定了相同的 validationKey 和验证算法。不能在群集中使用 AutoGenerate。

Monday, May 15, 2006 4:21 AM by longren629

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

Thank you!
Thursday, May 18, 2006 12:20 PM by Randy Jackson

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

Check this out:
http://www.andreas-kraus.net/blog/validation-of-viewstate-mac-failed/

It might be the solution until it's officially fixed.
Thursday, June 22, 2006 7:15 AM by Andreas Kraus

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

<pages enableEventValidation="false" viewStateEncryptionMode="Never"/>
did not solve my problem, but
<pages enableEventValidation="false" viewStateEncryptionMode="Never" enableViewStateMac="false"/>
did ;)
Saturday, June 24, 2006 7:25 AM by Infinum

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

Thanks Infinum, that solve my problem also!
Wednesday, July 12, 2006 11:15 AM by masa

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

Esto resolvió mi problema

<pages enableEventValidation="false" viewStateEncryptionMode="Never" enableViewStateMac="false"/>
Thursday, July 13, 2006 11:18 AM by buitre

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

I have a very basic aspx page. I do not have any controls apart from some text boxes and just one form. I get this error when I leave the page idle for long. I do not think it occurs when session expires because I made the session timeout for 1 min, but still it occurs when I leave the page idle for more than 20 minutes... any clue whats going on?
Tuesday, July 18, 2006 10:28 AM by Anil

# 网站测试中出现 验证视图MAC失败

在网站测试中出现了验证视图MAC失败
验证视图MAC失败。如果此引用程序由网络场或群集承载,请确保&amp;lt;machineKey&amp;gt;配置指定了相同的 validationKey 和验证算法。不能在群集中使用...
Thursday, July 20, 2006 2:43 AM by Joe's Blog

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

Thanks ... it was perfectly, but is there any disadvatatges of disabling validation ?
Wednesday, August 02, 2006 9:00 AM by Laith Murad

# Validation of viewstate MAC failed

hi, i used atlas in a page and users of my website got below error:Validation of viewstate MAC failed.
Friday, August 04, 2006 3:00 PM by ATLAS Forum Posts

# 验证视图MAC失败 Validation of ViewState MAC Failed

验证视图MAC失败 Validation of ViewState MAC Failed

Tuesday, October 17, 2006 11:38 PM by aboutblog

# DasBlog 1.9 Fehler beim Anmelden: Viewstate MAC failed

Tuesday, November 28, 2006 7:02 AM by werner - oder was ?

# ASP.NET misleading &quot;Validation of viewstate MAC failed&quot;

Friday, December 29, 2006 4:22 PM by VS and .NET crazy

# TFS Testing

TFS Testing

Wednesday, June 27, 2007 9:04 AM by Mark Wiggins

# 验证视图MAC失败 Validation of ViewState MAC Failed

验证视图MAC失败 Validation of ViewState MAC Failed

Sunday, July 01, 2007 4:54 AM by 天纯蓝

# 验证视图MAC失败 Validation of ViewState MAC Failed

今天在调试Atlas时遇到错误: 验证视图MAC失败。如果此引用程序由网络场或群集承载,请确保配置指定了相同的 validationKey 和验证算法。不能在群集中使用 AutoGenerate

Saturday, July 21, 2007 2:37 AM by yyhzjw

# Potential solution for "Validation of viewstate MAC failed"

Last year, I blogged about the potential for &quot;Validation of viewstate MAC failed&quot; issue in

Tuesday, August 14, 2007 3:19 PM by Joteke's Blog

# 验证视图MAC失败 Validation of ViewState MAC Failed

验证视图MAC失败ValidationofViewStateMACFailed 今天在调试Atlas时遇到错误:

验证视图MAC失败。如果此引用程序由网络场或群集承载,请确保

Friday, August 24, 2007 1:48 AM by 龍龙

# Potential solution for "Validation of viewstate MAC failed"

Last year, I blogged about the potential for &quot;Validation of viewstate MAC failed&quot; issue in

Thursday, October 04, 2007 11:21 AM by ASPInsiders

# ASP.NET - Validation of viewstate MAC failed!

ASP.NET - Validation of viewstate MAC failed!

Monday, January 28, 2008 3:17 PM by brute forced brilliance

# L'errore Validation of viewstate MAC failed su UGI

L'errore Validation of viewstate MAC failed su UGI

Friday, February 08, 2008 9:22 AM by Around and About .NET World

# A few more solutions to the dreaded "Validation of ViewState MAC failed"

I posted about this some time ago. My previous post: Potential solution for &quot;Validation of viewstate

Sunday, March 16, 2008 12:44 PM by Joteke's Blog

# Tony YangYang&#8217;s Online Word &raquo; Blog Archive &raquo; Validation of ViewState Mac failed in ASP.NET 2

# 验证视图MAC失败 Validation of ViewState MAC Failed

验证视图MAC失败ValidationofViewStateMACFailed 今天在调试Atlas时遇到错误:

验证视图MAC失败。如果此引用程序由网络场或群集承载,请确保

Saturday, August 30, 2008 9:54 PM by zzh5945

# re: ASP.NET 2.0 and &quot;Validation of ViewState Mac failed&quot; exception

Thanks a lot. You solve my problem and save my life.
Thursday, October 23, 2008 9:13 AM by Kevin

# re: ASP.NET 2.0 and &quot;Validation of ViewState Mac failed&quot; exception

Hi every one, a simple more secure way to overcome this exception is to add a section to your Web.config file specialy for those who use web farms. for example (remove the previous changes to section first): for more info see MSDN help on machineKey element, have a nice dev :D
Friday, November 07, 2008 11:49 PM by M.Aghajani

# re: ASP.NET 2.0 and &quot;Validation of ViewState Mac failed&quot; exception

I found the current thread we are all reading on www.asp.net while I was searching for a problem with my membership and role providers. I kept getting the following 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. Auto Generate cannot be used in a cluster." The problem was my login system worked fine on my local machine, but when I FTPed my application to my remote server and then launched it, the user names and roles I created locally would not authenticate. Login would fail for these users. If I then created new users with my application on my remote server they would authenticate properly. In short, any time I moved my application to a new directory the users and roles created when the application resided in previous directories failed. I found a solution to the problem that worked for me. The solution is to manually add the applicationName attribute to the node of your web.config file and give it a value, such as "/". I wrote a short article about the process I used to fix the problem. Hope it helps: http://www.itegritygroup.com/itegrity/seo-web-development-blog/index.php/2008/10/validation-viewstate-mac-failed/
Sunday, November 30, 2008 11:02 PM by Steve Kozyk

# Simple Solution Found - IE7

My issue was specific to IE7. Turned out to be a setting in Internet Options, Browsing History, Settings. Under Temporary Internet Files, radio button was on "Never". Changed it to "Everytime I visit the webpage". Cleared it right up.
Friday, January 09, 2009 11:38 AM by PjS

# Fehler bei Validierung der ViewState-Mac - Indiz auf Angriff? | hilpers

# re: ASP.NET 2.0 and &quot;Validation of ViewState Mac failed&quot; exception

Thanks, good stuff
Tuesday, March 17, 2009 6:49 AM by Svarga

# re: ASP.NET 2.0 and &quot;Validation of ViewState Mac failed&quot; exception

I was having this problem with a Master-page scenario. The master page contains a form [a quick search] whose action was to call a search page. To solve the problem -- I simply removed the "runat='server'" directive from the form tag. It solved my problem...
Thursday, March 26, 2009 4:21 PM by Greg Patnude

# re: ASP.NET 2.0 and &quot;Validation of ViewState Mac failed&quot; exception

I have an asp page that posts to itself and has a javascript submit that sets the action. After posting to itself, it redirects based on action. I recently redirected my page to a asp.net 2.0 web page and got this error. will try the solution with web.config thanks sam
Tuesday, April 07, 2009 5:59 PM by Sam

# re: ASP.NET 2.0 and &quot;Validation of ViewState Mac failed&quot; exception

"Simple Solution Found - IE7" - Fixed it for me. Thank you PjS
Wednesday, April 22, 2009 6:33 AM by Tom

# 验证视图MAC失败 Validation of ViewState MAC Failed

原文地址:

Monday, April 27, 2009 5:27 AM by Kyle Wong

# ?????? ?????????????????? MAC ???????????????????????? | WaterG's Blog

# re: ASP.NET 2.0 and &quot;Validation of ViewState Mac failed&quot; exception

I´m having this problem but not on a web farm. Actually, the server is a virtual machine exclusive for this application. My problem is that I have a Reporting Services solution who needs a form authentication so I can open the reports from a website. I can´t allow anonymous access because some of the reports are private. So i´m using the form authentication sample on SQL Server 2005 CD. The problem is that when I post the parameters to the report this error shows up. I tried to change the web.config of the reportserver folder and nothing. I think that the real problem is that i´m changing the action of the form with javascript and that causes a different viewstate. Someone?
Wednesday, May 06, 2009 5:27 PM by Rafael Bandeira

# re: ASP.NET 2.0 and &quot;Validation of ViewState Mac failed&quot; exception

Somehow started working after I reboot the VM. []´s
Monday, May 11, 2009 2:50 PM by Rafael Bandeira

# re: ASP.NET 2.0 and &quot;Validation of ViewState Mac failed&quot; exception

hey joteke, Thank You!! very much, I was getting same error and your solution worked for me. I have 60 server controls. Thanks Upen
Monday, May 18, 2009 3:39 PM by Upen

# re: ASP.NET 2.0 and &quot;Validation of ViewState Mac failed&quot; exception

i became this error, when the action-attribut of the form-tag, has the value of a other page.
Wednesday, June 03, 2009 8:00 AM by shukow

# re: ASP.NET 2.0 and &quot;Validation of ViewState Mac failed&quot; exception

Thank you! Thank you! Thank you! You just saved me hours.
Tuesday, June 23, 2009 8:42 PM by Brent

# re: ASP.NET 2.0 and &quot;Validation of ViewState Mac failed&quot; exception

valeu obrigado! isso resolveu meu problema.
Thursday, August 13, 2009 10:29 AM by Fabio

# Validation of viewstate MAC failed. &laquo; Randomos Technos

Tuesday, September 08, 2009 7:27 PM by Validation of viewstate MAC failed. « Randomos Technos

# fix bad credit

I enjoyed this post - thank you for sharing it with everyone!

Monday, September 14, 2009 3:22 AM by fix bad credit

# re: ASP.NET 2.0 and &quot;Validation of ViewState Mac failed&quot; exception

This fixed it for me: In the page directive at the top of the aspx page I added these two items. EnableViewStateMac="false" EnableEventValidation="false I set EnableViewState="False" in any control on the page that causes the viewstate issues. Mine works now.
Thursday, October 22, 2009 6:27 PM by Dale Bonamie

# re: ASP.NET 2.0 and &quot;Validation of ViewState Mac failed&quot; exception

Wow! Figured something out on this.. (A bit late, but good none the less) If you are submitting the form via Javascript and get this error, make sure you have another button SOMEWHERE on the form with a PostBackURL assigned. Not sure why, but that fixed the issue for me.
Wednesday, December 16, 2009 4:10 PM by Sammy

# re: ASP.NET 2.0 and &quot;Validation of ViewState Mac failed&quot; exception

Ran into this exception today. Our problem was that we used a BasePage for all our pages where the Form action is set to rawurl. Works fine, but threw this exception when we did a Server.Transfer betweeen two pages. The Form action for the destination page was set to rawurl, i.e the first page. So a postback triggered in the destination page was in pointed to first page. A bit confusing maybe, but hope it helps. //A
Thursday, December 17, 2009 9:12 AM by Ai Bui

# fix your credit

I have to add this website to my favorites!

Monday, January 18, 2010 6:20 AM by fix your credit

# acai berry maxx

Entrepreneurs face a multitude of challenges when setting up and growing their business venture. This is even more so for entrepreneurs in emerging markets and developing countries. Access to finance is often the main challenge. The Venture Capital and

Saturday, February 06, 2010 7:35 PM by acai berry maxx

Leave a Comment

(required) 
required 
(required) 
Enter the code you see below