A solution to: Validation of viewstate Mac failed

Source: Internet
Author: User
Original post address: http://forums.asp.net/1173230/ShowPost.aspx

Validation of viewstate Mac failed/the state information is invalid for this page and might be specified upted

Hey folk,

Maybe you can help me with the following prob:
I developped a "job search application" in Asp.net 2.0 Beta. it compiled and ran without any problems. now, with the new Asp.net 2.0 final version, I'm getting a lot this error message:

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.

This error is only generated if you navigate quickly through the Web application. that means, if you press an <asp: button> before the whole page is loaded/Rendered, then you get this posted error. if you wait until the whole page has finished rendering, then the PostBack generates no errors.
 
I 've read a lot of articles and Forum posts, and nothing figured out the problem:
-No, I'm not running a web farm.
-Page property "enableviewstatemac" set to false does not solve the problem, it just changes the error messageIn same navigation behaviour:

The State information is invalid for this page and might be upted.

The second error cocould be solved with hotfixes/updates in Asp.net 1.0/1.1
Is there also a solution available for Asp.net 2.0 final version?

Thanks for helping!

further information about my problem:

I 've done a lot of test cases in recently past hours, following I found out concerning my problem:
it depends on which webcontrols you have in use in an Asp.net 2.0 final application . my reported problem only occurs if you 've nested a gridview, A detailview or a formview within *. ASPX page. and it does not even matter if they are bound to a datasource control. they fail in both cases (bound/unbound) If you navigate too quickly (have a look at first post for its explanation ).

Conclusion:
My problem has too be a general one in Asp.net 2.0 final framework. i'm working in a small developping team for Web solutions. and we 've figured out that all our Asp.net applications (which were running fine in Asp.net beta 2.0) have this problem error message "validation of viewstate Mac failed" occurs in simple page architecture (no masterpages, no callbacks etc .) even!
I'm sure other developers are also hit, aren't they?

Special remark:
Although we have no webfarm in use, I 've also tried to use a self-generated <machinekey> in Web. config. No success as expected ;-)

What a 'tiny' bug...

Today, I found out under which exact circumstances the reported error raises.
It all belongs to the datakeynames property!

If you don't use this property, you will never get that "validation of viewstate Mac failed" errorNavigating quickly(Do PostBack before whole page has finished rendering ).
So, all you need on an *. ASPX page for generating this error:

    1. Sqldatasource
    2. Gridview bound to sqldatasource
    3. Using datakeynames in gridview
    4. Something on the *. ASPX page which takes a while till rendered (as for example banners, external pictures etc .)
    5. Button *

* You can also do a PostBack with the gridview's sorting command.

Final conclusion:
That must be a bug in the. NET Framework! MVP users or others... what do you think about?

Hey joteke,

Thanks for taking time! I 've spent the morning for reproducing a small sample page that points out the reported bug. I used a <asp: xmldatasource> instead of <asp: sqldatasource> for make it independent of any configuration ressources. you can download the source here: http://www.espace.ch/viewstateProblem.rar

You have to click very quickly (serveral times) in doing the PostBack for reproducing the bug in the sample page !!!

For those guys who think the navigation behavior strange... yeah, you're right: in the given sample page, it is really silly n' difficult navigating so quickly (pressing button before page has finished rendering !!). But if you look at the final application (http://adv.espace.ch/einsteigerprofil), you'll see that reported navigation behavior is common: The standard shape/Design of ESPACE. ch takes an amount of time for loading-and I don't think users are gonna wait until page has finished rendering each time.

Last but not least I have to correct my observations!
So, all you need on an *. ASPX page for generating this error:

    1. Any datasource control (sqldatasource, xmldatasource ...)
    2. Gridview bound/Unbound to datasource
    3. Using datakeynames in gridview
    4. Using <asp: boundfield> Elements
    5. Something on the *. ASPX page which takes a while till rendered (as for example banners, external pictures etc .)
    6. Button

OK,

If you guys add this to Web. config, does it still reproduce?

<Pages validaterequest = "false" enableeventvalidation = "false"Viewstateencryptionmode = "never"/>

E. g note the viewstateencryptionmode.

Hi joteki,

the error is ever present in my case. I get the typical "invalid PostBack or callback argument. event validation is enabled using in configuration or <% @ page enableeventvalidation = "true" %> in a page. for security purposes, this feature verifies that arguments to PostBack or callback events originate from the server control that originally rendered them. if the data is valid and expected, use the clientscriptmanager. registerforeventvalidation method in order to register the PostBack or callback data for validation. "Whether enableeventvalidation is set to true or false.

One thing worth noting: My page actually has three different controls, all Seperate and none linked or nested-1 gridview and 2 datalists. if I take a new aspx, create a simple gridview bound to the same view in SQL, and try sorting (my problem occurs on the SORT), Everything rolls fine. but in my original. aspx, I get the error.

So I removed the controls in order to slowly start "removing possible conflicts" And suddently, the sort in my gridview worked without a hitch. I put the datalist back and the error returned.

Thinking I was on to something, I removed all datalists and gridview from my original. aspx as well as sqlsources. I then proceeded to add my gridview and the error returned without the datalists.

This really appears to be a bug in. NET 2.0, and I'm 've yet to figure out why exactly it happens. isolating it appears impossible.

The only workaround I can suggest is to "think outside of the box" and try placing your controls in other ways, Or redo the page altogether.

I have had other bugs where a simple bound dropdownlist wocould not function properly until I literally "deleted" The usercontrol (. ascx) from my project, created a new one and the DDL functioned properly moving forward.

Servicepack anyone?

/JD

Hi everyone,

The error that's happening (as has been mentioned earlier) 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 got.

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 und 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 cocould disable the UI until the form has rendered?

Unfortunately there's no "perfect" solution for this just yet.

Thanks,

Eilon

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.