Click here to Skip to main content
15,313,682 members
Articles / Web Development / ASP.NET
Technical Blog
Posted 8 Nov 2009

Tagged as


7 bookmarked

Weird Error: Unable to Validate Data in ASP.NET website

Rate me:
Please Sign up or sign in to vote.
4.89/5 (7 votes)
8 Nov 2009CPOL2 min read
A weird error that comes when a page is opened for a long time and then posted back

Have you ever come across a situation where your website which was working for the last couple of months gives a weird error "Unable to Validate Data". Yesterday, while working I found that my website which is already published in IIS throws this error. Initially I thought the error might be with my code, but I found that everything is good with the code. So I looked forward to internet and found that this error comes when the viewstate of a page cannot be decrypted when the response is received from the client.

When I looked at where the error was occurring (Target Site) I found:

Unable to validate data at
System.Web.Configuration.MachineKey.GetDecodedData(Byte[] buf, Byte[] modifier,
Int32 start, Int32 length, Int32& dataLength) at
System.Web.UI.LosFormatter.Deserialize(String input) 

Actually the problem is with the viewstate. The viewstate is actually decrypted in the server using a secret Machine key which resides on the server. The interesting thing is the key gets regenerated after a certain time. Therefore when the user returns the viewstate, if the machine identified key is changed, the decryption of viewstate fails and thus throws this nasty error.


The solution is simple. First of all, to solve the issue, I disabled the ViewState for the current page by putting EnableViewState = false. I even disabled this for the entire viewstate for the website using Web.config. But still the error.
Finally I used "EnableViewStateMac =false" in pages section. Voila, this cures the problem.

<pages buffer="true" enableViewStateMac="flase"/>

Just place the following between the system.web section and the site starts working.

Another solution that you might use as well is to place the machine key directly on your web.config, so that it always decrypts and encrypts using the static key values. To do this, you need to use the following:

<machinekey validationkey="22E995276703B846C4AD0881EE3159FFCB376CD48B27F64
124BAADEE85A857CC135BC" decryptionkey="60588EB661A6483348C20F92659775872CB06427AF20733C" 

You might use this site to Generate your validation key as well.
To get deep knowledge on what makes this happen, I found some insight from the Internet and read some articles of MSDN. Let us talk a little on that note.
Say you made a request for a page in the server. After you place the request the server processes it, encrypts the viewstate that the server receives using the encryption mentioned. Basically it uses the key mentioned in the Machine.config to encrypt the viewstate data. Finally it converts to Base64 and embeds into some hidden fields.

We can mention the machine key in Web.config too so that it uses it for the current website. You might use the AutoGenerate option too to enable/disable autogeneration of key during runtime.

Your comments are welcome.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Abhishek Sur
India India
Did you like his post?

Oh, lets go a bit further to know him better.
Visit his Website : to know more about Abhishek.

Abhishek also authored a book on .NET 4.5 Features and recommends you to read it, you will learn a lot from it.

Basically he is from India, who loves to explore the .NET world. He loves to code and in his leisure you always find him talking about technical stuffs.

Working as a VP product of APPSeCONNECT, an integration platform of future, he does all sort of innovation around the product.

Have any problem? Write to him in his Forum.

You can also mail him directly to

Want a Coder like him for your project?
Drop him a mail to

Visit His Blog

Dotnet Tricks and Tips

Dont forget to vote or share your comments about his Writing

Comments and Discussions

QuestionThanks for the explanation Pin
VC Sekhar Parepalli21-Nov-13 13:57
MemberVC Sekhar Parepalli21-Nov-13 13:57 
Hi Abhishek,
Appreciate the explanation; but what, in the server or network or browser etc.., makes this data-validation-failure to trigger in the first place? Sniff | :^)

My Situation: I came across this issue in a Security-Tightened-customer's IIS Server and am supposed to install my based application (custom user authentication where username and password will be in a table on DB) there.

At my Development Server: The application installed successfully, lets me log in successfully, and I could see the first screen and navigate to all other screens with out issues. Smile | :)

At Customer's Server: The application installed successfully and lets me log in successfully. But I could not see the first screen. Sigh | :sigh:

My observations: After reading your article, I tried going to other screens of my web-application by typing the url directly. Wherever the ViewState variable is not populated, those screens rendered perfectly as designed. Wherever the view state variable carries some value, those pages displayed this weird error. D'Oh! | :doh:

Then I tried the below.
1) disabled the viewstate through out the site, and the error still remained there.
2) added the IIS-generated keys directly to the web.config, restarted IIS, and the error still remained there.

No matter what this error doesn't seem to go away. D'Oh! | :doh:

Any tips that you can share would be appreciated.


AnswerRe: Thanks for the explanation Pin
VC Sekhar Parepalli22-Nov-13 5:56
MemberVC Sekhar Parepalli22-Nov-13 5:56 
QuestionMachineCode in web.config Pin
ankeshjain198926-Aug-13 19:49
Memberankeshjain198926-Aug-13 19:49 
AnswerRe: MachineCode in web.config Pin
Abhishek Sur27-Aug-13 1:07
professionalAbhishek Sur27-Aug-13 1:07 
GeneralRe: MachineCode in web.config Pin
ankeshjain198931-Aug-13 2:53
Memberankeshjain198931-Aug-13 2:53 
GeneralMy vote of 4 Pin
stuy29-Oct-12 13:14
Memberstuy29-Oct-12 13:14 
GeneralGood Work Pin
ranjan_namitaputra13-Nov-09 8:30
Memberranjan_namitaputra13-Nov-09 8:30 
GeneralNote Pin
Sourav_9i13-Nov-09 0:11
MemberSourav_9i13-Nov-09 0:11 
GeneralThank you! Pin
dlcodeproject9-Nov-09 4:22
Memberdlcodeproject9-Nov-09 4:22 
GeneralRe: Thank you! Pin
Abhishek Sur9-Nov-09 8:38
professionalAbhishek Sur9-Nov-09 8:38 
GeneralGuys Pin
Abhishek Sur8-Nov-09 11:12
professionalAbhishek Sur8-Nov-09 11:12 
GeneralRe: Guys Pin
rtpHarry29-Dec-11 11:55
professionalrtpHarry29-Dec-11 11:55 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.