views:

2914

answers:

7

I don't know if anyone has seen this issue before but I'm just stumped. Here's the unhandled exception message that my error page is capturing.

Error Message: 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.

Stack Trace: at System.Web.UI.ViewStateException.ThrowError(Exception inner, String persistedState, String errorPageMessage, Boolean macValidationError) at System.Web.UI.ObjectStateFormatter.Deserialize(String inputString) at System.Web.UI.ObjectStateFormatter.System.Web.UI.IStateFormatter.Deserialize(String serializedState) at System.Web.UI.Util.DeserializeWithAssert(IStateFormatter formatter, String serializedState) at System.Web.UI.HiddenFieldPageStatePersister.Load() at System.Web.UI.Page.LoadPageStateFromPersistenceMedium() at System.Web.UI.Page.LoadAllState() at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) at System.Web.UI.Page.ProcessRequest(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) at System.Web.UI.Page.ProcessRequest() at System.Web.UI.Page.ProcessRequestWithNoAssert(HttpContext context) at System.Web.UI.Page.ProcessRequest(HttpContext context) at ASP.generic_aspx.ProcessRequest(HttpContext context) at System.Web.HttpApplication.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() at System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously)

Source: System.Web

Anybody have any ideas on how I could resolve this? Thanks.

+10  A: 

I seem to recall that this error can occur if you click a button/link etc before the page has fully loaded.

If this is the case, the error is caused by an ASP.net 2.0 feature called Event Validation. This is a security feature that ensures that postback actions only come from events allowed and created by the server to help prevent spoofed postbacks. This feature is implemented by having controls register valid events when they render (as in, during their actual Render() methods). The end result is that at the bottom of your rendered form tag, you'll see something like this:

<input type="hidden" name="__EVENTVALIDATION" id="__EVENTVALIDATION"  value="AEBnx7v.........tS" />

When a postback occurs, ASP.net uses the values stored in this hidden field to ensure that the button you clicked invokes a valid event. If it's not valid, you get the exception that you've been seeing.

The problem you're seeing happens specifically when you postback before the EventValidation field has been rendered. If EventValidation is enabled (which it is, by default), but ASP.net doesn't see the hidden field when you postback, you also get the exception. If you submit a form before it has been entirely rendered, then chances are the EventValidation field has not yet been rendered, and thus ASP.net cannot validate your click.

One work around is of course to just disable event validation, but you have to be aware of the security implications. Alternatively, just never post back before the form has finished rendering. Of course, that's hard to tell your users, but perhaps you could disable the UI until the form has rendered?

from http://forums.asp.net/p/955145/1173230.aspx

Chris Driver
So if I just disable all the controls before until the render event completely finishes then this should solve it right?
orlando calresian
Yes, that would work, but more specifically disable controls that cause a postback.
Chris Driver
Thanks for the help Chris.
orlando calresian
+3  A: 

do you have multiple servers running this application and/or have a web garden? If yes, you are going to have to set the machine key in the web.config

Darren Kopp
A: 

I know you can disable the Validation of viewstate MAC, but I think if the page is not loaded you can get into more trouble. When I ran into this problem I had to disable all buttons until the page was fully loaded.

Eduardo Campañó
+7  A: 

@Chris

if the problem is clicking an item before the page has completely rendered, asp.net 3.5 SP1 added a web.config entry on the page element called renderAllHiddenFieldsAtTopOfForm.

Darren Kopp
Good tip Darren, I'll remember that when I eventually get to code in asp.net 3.5 :)
Chris Driver
Darren, I appreciate your response but I'm running a asp.net 2.0 application. Also, it looks like I don't have asp.net 2.0 sp1 just 2.0 so the PagesSection datatype isn't available unfortunately.
orlando calresian
2.0 has the pages section, it's <pages />. It's under <system.web>. It's in asp.net 2.0 because on it you can define things like master page and theme.
Darren Kopp
Darren, I looked and and there wasn't any property called RenderAllHiddenFieldsAtTopOfForm in the page element of web.config for asp.net 2.0. There were other properties however that could fix the issue but I don't want to disable event validation. Thanks for the tips.
orlando calresian
@orlando you have to have .net 3.5 SP1. that's when it was added.
Darren Kopp
+1  A: 

By default, ASP.NET includes a digital signature of the ViewState value in the page. It does so with an automatically-generated key that is held in memory. This is done to prevent a malicious user from altering the ViewState from the browser and, for example, grant him/herself access to stuff they wouldn't normally have access to.

ASP.NET can also, optionally, encrypt the ViewState, but it's turned off by default for performance reasons. In many web sites, it is a lot more important to make sure that the content of the ViewState is not 'mucked with', than it is to keep it confidential.

The error message says that the signature verification failed. The page was posted with a ViewState, but the ViewState signature didn't match the signature calculated with the keys held by the server.

The most common reason for this error is that you are using two or more web servers in a farm-like environment: one server sends the original page, signed with the key in memory on that server, but the page is posted back to the second (or third...) server. Because the two or more servers don't share the signature key, the signatures don't match.

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

What the error message is telling you is to use the validationKey attribute (see details in MSDN) in your web.config to hardcode the signature key to a value shared by all your servers, instead of using a dynamically-generated one. That way, the signature validation can succeed independently of which server receives the postback.

You could turn off the verification, but it's very dangerous to do so. It means any hacker with a bit of free time can fake values in your application. For example, if you keep the price of the item in a ViewState value, the hacker could change the value from the browser to $0.01 right before putting the order.

Euro Micelli
I appreciate your response but this app is only hosted on a single server not a cluster or a web farm.
orlando calresian
A: 

For anyone else ending up struggling with this issue here is a helpful link to some work arounds:

http://blogs.msdn.com/tom/archive/2008/03/14/validation-of-viewstate-mac-failed-error.aspx

orlando calresian
A: 

I had same problem.

I used MicrosofrReportViewer Control in my ASP.NET MVC application.

And I got same exception like you

So, I tried a lot of ways as we know, However it did not work for my case.

Finally, in my case following way does work successfully.

http://www.codeproject.com/KB/viewstate/ViewStateCompression.aspx

I hope this help you.

Kwon