Invalid ViewState
I’ve been working with a customer who is working with my application as a typical end user would. This means, they often use the browser in ways the application isn’t meant to be used <g>. If you want to translate that: It means they use the BACK button a lot.
One of my application pages is fairly complex and it contains about 50 fields or so which are displayed with a tabbed display and used to display inventory data.
I’ve been getting a ton of Invalid Viewstate errors which looks like this in my error log:
/store/admin/EditInventoryItem.aspx?Sku=KWR_TSHIRT_BW_ORANGE
Invalid_Viewstate
Client IP: 68.116.115.30
Port: 47723
User-Agent: Mozilla/5.0 (Macintosh; U; PPC Mac OS X; en) AppleWebKit/312.1 (KHTML, like Gecko) Safari/312
ViewState: dDwtMTc0Nzg0MDU5OztsPFd3d2ViY2hlY2tib3gxO3R4dG5vd2ViO1d3d2ViY2hlY2tib3gyOz4+wsDm0DO4DrTgsEuGylUVMawoGG0=
Http-Referer: http://www.kidswhorip.com/store/admin/EditInventoryItem.aspx?Sku=KWR_TSHIRT_BW_ORANGE
Path: /store/admin/EditInventoryItem.aspx.
on
--- Stack Trace ---
at System.Web.UI.Page.LoadPageStateFromPersistenceMedium()
at System.Web.UI.Page.LoadPageViewState()
at System.Web.UI.Page.ProcessRequestMain()
--- Request Information ---
Full Url: http://www.kidswhorip.com/store/admin/EditInventoryItem.aspx?Sku=KWR_TSHIRT_BW_ORANGE
IP: 68.116.115.30
Referer: http://www.kidswhorip.com/store/admin/EditInventoryItem.aspx?Sku=KWR_TSHIRT_BW_ORANGE
Browser: Mozilla/5.0 (Macintosh; U; PPC Mac OS X; en) AppleWebKit/312.1 (KHTML, like Gecko) Safari/312
Login: WEB\rod
Locale: en
--- Raw Post Buffer ---
__EVENTTARGET=&__EVENTARGUMENT=&__VIEWSTATE=dDwtMTc0Nzg0MDU5OztsPFd3d2ViY2hlY2tib3gxO3R4dG5vd2ViO1d3d2ViY2hlY2tib3gyOz4%2BwsDm0DO4DrTgsEuGylUVMawoGG0%3D&txtsku=KWR_TSHIRT_BW_ORANGE&txt _EditTabs=Detail
... additional data removed for brevity
I’ve seen the errors happening on the user’s browser configuration but no matter how I try here I can’t duplicate a repeatable scenario.
What’s even worse is that this page has ViewState disabled – my app doesn’t need or use it, so the viewstate used is only in use for ASP.NET internally.
After some more searching I ran intot he following KB Article:
Intermittent Invalid Viewstate Error in ASP.NET Web pages
This article suggests that the error might occur because of Application Pool cycling. The solution to this problem is to set a flag in machine.config to prevent ASP.NET to generate a new key everytime an Application starts.
I tried to get this to fail, simply by restarting IIS, but that didn’t do it for me – I got no failure, so I’m still not quite sure what is causing the Invalid ViewState scenario to occur.
The first thing I’m trying right now is to set EnableViewStateMac=”false” in the page and see if that helps. I suspect this will fix the problem, but I’d still like to know why the validation is failing on the host server.
Other Posts you might also like
The Voices of Reason
# re: Invalid ViewState
I had a similar problem in an environment using clustered machines. It seemed that the user was getting responses from different machines of the cluster, so the ASP.NET was thinking that the viewstate data was corrupted. Setting EnableViewStateMac=”false” solved the problem, but the real solution was actually change the <MachineKey> element to use the same hash generation key on both machines of the cluster.
I don't know if this scenario applies to your case, but it was really the problem that bothered me for some days.
# re: Invalid ViewState
I get this all of the time when someone is visiting one of my large webapplication sites using Pocket IE. This is not uncommon since the web site integrates with the PPC using SOAP and so a user will often log on to see the data that they have collected.
It has not yet become important enough to warrent further investigation - I have just been putting it down to PIE being buggy.
# re: Invalid ViewState
# Another way to fix the pooling/cycling problem
# re: Invalid ViewState
# re: Invalid ViewState
Specifically, in Safari only:
1. window.open("page1.aspx")
2. page1.aspx has form with __VIEWSTATE
3. Do normal hyperlink from page1.aspx to page2.aspx (i.e, don't do a postback)
4. On page2.aspx, perform an operation that does a postback
5. See viewstate error -- we note that the viewstate sent to the server was from the form in page1.aspx, not page2.aspx.
6. If you put an alert at the bottom of page2.aspx, to display __VIEWSTATE from forms[0], you will see the viewstate of the previous page's form, but if you do a view source of page2.aspx, you will see the correct view state.
We are doing no server transfers or anything like that.
In Safari 1.0, but no longer, we saw the same problem, in the case where step 3 was replaced by page1.aspx emitting javascript that caused an immediate page reload to page2.aspx(e.g., "window.location = 'page2.aspx'")
Note that the <form> tags in each of these pages have unique names and ids, and that the size of viewstate is 2-4K.
# re: Invalid ViewState
It's been my experience that 99% of the users who receive this error are using the Mac PowerPC or mobile browsers. Rarely does a user who is running IE 6.0 encounter the error.
# re: Invalid ViewState
http://weblogs.asp.net/pwilson/archive/2003/08/21/24867.aspx
I've also noticed that it only appears to happen when doing a redirect (not a transfer).
# re: Invalid ViewState
# re: Invalid ViewState
A user logins into the site they browse around. They leave the session idle and then return. Their session/authentication has expired so they are redirected to our login page to re-authenticate. The previous pages viewstate is also passed to the login page causeing it to fall on its face.
We see this with Safari/417.8 - Safari/417.92 and WebTV/2.5.9
I have yet to find a solution... :(
# re: Invalid ViewState
I agree with Tim Farely's solution above.
I highly recommend everyone read (http://www.aspnetresources.com/articles/ViewState.aspx#) this excellent article about this exact problem.
Good luck.
# re: Invalid ViewState
ViewState security makes sense in certain scenarios but in most I don't see what the fuss is about. Heck most of our apps POST data to the server that's not encrypted and you can do a lot more damage with that than with ViewState (again unless you put sensitive data into ViewState).
# re: Invalid ViewState
http://aspalliance.com/909.
Also, if your server is at an ISP, you can put a machineKey into the web.config instead. I have a machingeKey generator at
http://www.developmentnow.com/articles/machinekey_generator.aspx
if anyone needs one.
# re: Invalid ViewState
I have used two methods to open a new window in which application runs.
Below is the C# code used my me.
Method 1:
Page.RegisterStartupScript("AppicationInNewWindow","<script language=javascript>OpenApp(url)</script>");
The OpenApp() is a javascript function which opens the Appication into new window using window.open.
Method 2:
Page.RegisterStartupScript("ChangeURL","<script language=javascript>window.location.href=url</script>");
In this method application is already opened in new window, so i have used window.location.href to change the url.
Both these methods are working fine for IE and Mozilla browsers. But Safari users get Invalid viewState error in Method 2. I have tried making EnableViewStateMac off for this particular page. But then page does not get postback event for first time. Please suggest any way to get rid of this error. Ur help would be greatly appreciated.
# re: Invalid ViewState
# re: Invalid ViewState
I have an HTTP module that handles all unhandled exceptions, so putting something like this in the ErrorHandler should do the trick :
public void HandleError (object sender, EventArgs e) {
HttpContext context = HttpContext.Current;
if (theError.Message.IndexOf("The client disconnected") == -1)
//handle the error
else
\\ignore it
}
Hope that helps!
# re: Invalid ViewState
http://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=101988
Back button:
Being a Java Spring developer also, this is the spring solution - pretty sure same rules apply: Back button problem is not a problem if the posted page does an http redirect to the success page. Then pressing back does not re-post, it really goes back. success page does an http GET ?id=savedid to show the results of the post for example.
Jol.
# re: Invalid ViewState
protected void Page_Load(object sender, EventArgs e) { if (Request.Browser.MSDomVersion.Major == 0) // Non IE Browser?) { Response.Cache.SetNoStore(); // No client side cashing for non IE browsers } }
# re: Invalid ViewState
# re: Invalid ViewState
Did anyone try changing the DOCTYPE in the page from XHtml to Html? I have read that this may also cause the Invalid Viewstate error for certain browsers:
http://www.31a2ba2a-b718-11dc-8314-0800200c9a66.com/2009/04/error-scriptresourceaxd-invalid.html
# re: Invalid ViewState
# re: Invalid ViewState
Is there a good method to prove or disprove this hypothesis?
# re: Invalid ViewState
I would suspect other things first. ViewState corruption can occur due to timeouts as well as AJAX stuff in pages messing with ViewState'd content. A lot of viewstate errors come from robots that access pages with cached data that has expired or is just plain invalid.
# re: Invalid ViewState
When the error occurred it always happened on the same page and the same asp.net event.
The patch that was offered to help track down the problem caused more problems because it broke other asp.net applications, not sure why.