views:

643

answers:

4

Hi,

I just added asp.net calendar control in new page under sample folder in asp.net mvc beta application. when i execute the particaular page that i need and it shows the error following

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. Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

that points to here . It's in site.master of asp.net mvc

        <div id="logindisplay">
            <% Html.RenderPartial("LoginUserControl"); %>
        </div>

usually to avoid this error, we give

but, it doesn't work for me.

pls help me.

Regards, Ambika

+1  A: 

Before reading further, please exclude the following preconditions:

  1. You aren't using a web farm.
  2. It appears when using built-in databound controls such as GridView, DetailsView or FormView which utilize “DataKeyNames”.
  3. 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 and the page hasn't loaded completely in client browser, you might get the "Validation of ViewState MAC failed" exception.

When this happens, if you just try setting the page property "EnableViewStateMac" to false, it does not solve the problem, it just changes the error message in same navigation behavior:

The state information is invalid for this page and might be corrupted.

This exception appears because Controls using DataKeyNames require Viewstate to be encrypted. When Viewstate is encrypted (Default mode, Auto, is to encrypt if controls require that, otherwise not), Page adds field just before closing of the <form> tag. But this hidden field might not have been rendered to the browser with long-running pages, and if you make a postback before it does, the browser initiates postback without this field (in form post collection). End result is that if this field is omitted on postback, the page doesn't know that Viewstate is encrypted and causes the aforementioned Exception. I.E. page expects to be fully-loaded before you 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. 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: . 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 above.

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

Workaround

Set enableEventValidation to false and viewStateEncryptionMode to Never as follows:

<pages enableeventvalidation="false" viewstateencryptionmode="Never">

This has the unwanted side-effect of disabling validation and encryption. On some sites, this may be ok to do, but it isn't a best practice, especially in publicly facing sites.

For more information and other workaround see this blog entry.

splattne
A: 

This happened to me just a few minutes ago. Luckily, I found this blog post on the subject. The upshot (for me, at least), was that I had two forms, one on the page and one on the control.

I didn't need the one on the page, so I removed it, and the error went away.

jrb
A: 
I have been struggling with this issue too since lasts few days and finally found the problem.

This error will show up in there are multiple <form> tags are being rendered on a page (be it html <form></form> or Html.BeginForm()).
Check the user controls being rendered, the content page section and also the Master page.
Make sure there is only one form rendered on a page.

This should fix your problem, if the issue persists, check for the for submit buttons rendered on the page (<input type="submit" …/>)

Cheers!
Mayank Srivastava