Friday, April 06, 2007

Validation of viewstate MAC failed.

            I was trying to post my aspx page to new page using JavaScript. The common methods "document.form1.submit()". I have specified action and method attribute also. Still I was getting error on form submit as shown below,

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

         Below is the code I was using for form submission.

document.form1.action="New.aspx"
document.form1.submit();

Fig - (1) Javascript to submit the form.

      I had written above script on submit button. After searching I found the reason for that. In ASP.NET, there is a specific method "__dopostback()" which allows user to submit the form to server(postback). Each time form is submitted with "post" method, CLR checks for valid viewstate so that there can not be any type of injection.

        To solve this problem you have set "name" attribute of viewstate before submitting. The code is shown below.

ocument.form1.action="New.aspx";
document.form1.__VIEWSTATE.name="name";
document.form1.submit();

Fig - (2) Javascript to submit the form without error.

Happy Programming !

5 comments:

deostroll said...

Hi,

Found this blog of urs really pertinent to my problem, however do you mean that __VIEWSTATE must be set to "name" or some other name like "New.aspx"

deostroll said...

Ok the "name" (as such) worked. I wonder why though!!!

Anonymous said...

You can try to find the answer here

Anonymous said...

Dont know why but this helps.. Thanks!!

Anonymous said...

Cool man.. you rock