Sunday, September 20, 2009

Invalid viewstate - Unable to validate data Exception

I was receiving following error message constantly in my error log.


Invalid viewstate. Client IP: 127.0.0.1 Port: User-Agent: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0; GTB6; SLCC1; .NET CLR 2.0.50727; Media Center PC 5.0; .NET CLR 3.5.21022; FDM; .NET CLR 1.1.4322; .NET CLR 3.5.30729; .NET CLR 3.0.30729; 6696076703) ViewState: /wEPDwULLTE0NTg5OTI1MzJkGAEFHl9fQ29udHJvbHNSZXF1aXJlUG9zdEJhY2tLZXlfXxYCBRZjdGwwMCRDQyRjaGtSZW1lbWJlck1lBRZjdGwwMCRDQyRjaGtUZXJtc09mVXNl4IYD7Rs56CiQOVAEKcORk75X2xw= Referer: http://localhost:56095/HotLinks.aspx Path: /HotLinks.aspx

System.Web.HttpException: Unable to validate data. at System.Web.Configuration.MachineKeySection.GetDecodedData(Byte[] buf, Byte[] modifier, Int32 start, Int32 length, Int32& dataLength) at System.Web.UI.ObjectStateFormatter.Deserialize(String inputString)


Cause of this error:
My web application has multiple forms, and from each form if user wants to post feedback and if he is anonymous user, he is redirected to login form.

To improve performance of web application i was using Server.Transfer to redirect anonymous user to login form.

As Server.Transfer don't change the url string, so even if user is on login form, it will still displays last url in browser, everything would be fine, until page is postback, if you do so than it would generate invalid viewstate error. As whenever you are doing page postback it needs to have correct url in order to post back to that page. But in this scenario, even though i am doing page postback on login form, its still assumes last page and so it end's up in above error message.

Work Around/Solution/Temporary Fix for Problem:
I couldn't able to find any good solution, but you can change your Server.Transfer call to Response.Redirect to fix the problem and move ahead.

I would update this post if i found any better solution to deal with this situation.

6 comments:

Jalpesh P. Vadgama said...

Hi Vivek,

If you are using the web server farm or load balancing server then you need to put same machine key for both load balancing server else
you need to put a viewStateEncryptionMode="Never" in the web.config. For more deatils see the following link.

http://forums.asp.net/t/1166634.aspx

Jalpesh P. Vadgama said...

Here is the another link

http://forums.asp.net/t/1063094.aspx

You need to put same machine key on all the server.

DotNetGuts said...

Thanks Jalpesh,

It was helpful.

Jalpesh P. Vadgama said...

Hi Vivek,

Are you able to slove this bug or not? Because i got the same bug and able to slove this by above way.

DotNetGuts said...

Nope i haven't tried, but i believe that would fix the problem. Let me update comment ones i am done...

Thanks

DotNetGuts said...

Jalpesh,

Just try to make changes and see if it works, but it didn't solve the problem. And I am sure that problem is not 100% solve for you too, As mentioned in blog scenario, when you try to redirect using server.transfer and page needs to be postback, than this would generate in error.

Most Recent Post

Community Updates

Subscribe Blog via Email

Enter your email address:



Disclaimers:We have tried hard to provide accurate information, as a user, you agree that you bear sole responsibility for your own decisions to use any programs, documents, source code, tips, articles or any other information provided on this Blog.
Page copy protected against web site content infringement by Copyscape