ASP.NET MVC Validation of viewstate MAC failed error

Posted in MVC | ASP.NET | .NET 3.5 at Saturday, December 13, 2008 2:30 AM UTC

If you get a Validation of viewstate MAC failed error on ASP.NET MVC, before trying to hard code your <machineKey attribute in web.config, disabling the keying of viewstate

<system.web>
  <pages enableViewStateMac="false" />
</system.web>

, or any other literature you may want to check if the page you are posting data from has two "<form" attributes (the one hard coded on the page and the one created with Html.BeginForm()). If you play with master templates and have to merge some MVC pages with tradicional ASP.NET you may get to that error.

It took me a while to find the solution...

Did this post save you some time? post it. I'm curious. :) thanks.

kick it on DotNetKicks.com AddThis Social Bookmark Button

Sunday, December 21, 2008 6:15:28 AM UTC
It sure did save me some time! Nice one, thanks!
Sunday, December 28, 2008 7:05:28 AM UTC
I wasted a couple of hours trying to find the issue, you saved my life, thanks!
Lloyd Phillips
Monday, December 29, 2008 4:14:15 PM UTC
Hi guys,
Nice to know that. It was just a minute to post it. I am glad it saved several hours of time...

ivan
Friday, January 30, 2009 12:03:50 AM UTC

I got the same error when i tried to manipulate the masterpage for passing the viewdata from content page. However reverting back the master page to the orginal version solved my problem.
However, it took me a while to solve the problem !
viren
Wednesday, March 11, 2009 5:42:38 AM UTC
Thanks for the help. I've used this twice now. You'd think I'd learn! :)
Wednesday, April 08, 2009 6:09:59 AM UTC
Chalk me up as another person who ran into this issue. I spent hours trying to troubleshoot it...and thankfully stumbled onto your post here. THANK YOU! THANK YOU! THANK YOU!
Steve
Tuesday, April 14, 2009 2:07:00 PM UTC
Nice one. I was down to my last monitor after smashing several with my keyboard... then I found your post.
Monday, April 27, 2009 10:31:21 PM UTC
woot. you rock.
Friday, May 22, 2009 5:43:42 AM UTC
2 hours i spent on this. damn, i should have googled a lot earlier
griegs
Saturday, June 20, 2009 10:39:07 PM UTC
Thanks! Removing the auto-generated form tag from my master page fixed the problem.
palmsey
Thursday, July 02, 2009 7:36:28 PM UTC
Thanks. this post saved a me lot of time.
Tiru
Saturday, July 11, 2009 9:31:16 AM UTC
Thought "Nah, cant be. I'm going to turn it off anyway". Stupid form tags in the site.master were causing it!
I'm sure I have broken something now... but it works so...

Cheers for the annoying solution.
Dan
Friday, July 31, 2009 8:57:54 PM UTC
I tracked the cause of this error down in my Web app to the use of the default page. So if I go to:

http://server/MyWebApp/

This loads Default.aspx, right? That's good, but then when I click on one of the link buttons on my sidebar, I get this "Validation of viewstate MAC failed..." error. Why? Because the action doesn't match the app path:

[ViewStateException: Invalid viewstate.
Client IP: 127.0.0.1
ViewState: /snip=
Referer: http://localhost/MyWebApp/
Path: /MyWebApp/default.aspx]

Notice the disjuncture between / and /default.aspx. I'm certain this is the issue, because if I go straight to http://server/MyWebApp/Default.aspx, everything works properly.

I fixed this simply by setting EnableViewState="false" on the aspx page in question. This worked in spite of the fact that the form tag is actually on my master page. It also worked because I don't have anything functional on my Default.aspx page other than a stack of link buttons, so if you have a very active form on your Default.aspx, this might be a problem. Otherwise it's a pretty simple solution. All of my other folders and pages use explicit naming, so there are no issues with lack of view state there.

Hope this helps someone else resolve this issue as well!
Wednesday, September 16, 2009 11:16:54 AM UTC
Thanks very much... this is very useful post. I tried a lot of things before this.
StrouMfios
Saturday, October 31, 2009 10:25:59 AM UTC
Great post!!! You saved my honor :-)
All comments require the approval of the site owner before being displayed.
Name
E-mail
(will show your gravatar icon)
Home page

Comment (Some html is allowed: a@href@title, b, em, i, strike, strong) where the @ means "attribute." For example, you can use <a href="" title=""> or <blockquote cite="Scott">.  

Enter the code shown (prevents robots):

Live Comment Preview

Yeap, the sponsors! :-)

On this page...

Tags

Calendar

<December 2008>
SunMonTueWedThuFriSat
30123456
78910111213
14151617181920
21222324252627
28293031123
45678910

Navigation

Archives

Recommended .NET blogs

The author

Iván Loire
Iván Loire (Zaragoza, Spain)

View Ivan Loire's profile on LinkedIn
Microsoft Certified Trainer
Send mail to the author(s) E-mail

Syndication

Feed Icon