开发者社区> 真爱无限> 正文

asp.net错误解决:Unable to Validate Data in ASP.NET website

简介: 原文:http://www.codeproject.com/Articles/43637/Weird-Error-Unable-to-Validate-Data-in-ASP-NET-web Have you ever come acr...
+关注继续查看

原文:http://www.codeproject.com/Articles/43637/Weird-Error-Unable-to-Validate-Data-in-ASP-NET-web


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 theviewstate, if the machine identified key is changed, the decryption ofviewstate fails and thus throws this nasty error.

Solution

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 usingWeb.config. But still the error.
Finally I used "EnableViewStateMac =false" in pages section. Voila, this cures the problem.

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

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 yourweb.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
9A074815C83D5C925038938F6943555687582CBC186DB22E552FCDB4D46
124BAADEE85A857CC135BC" decryptionkey="60588EB661A6483348C20F92659775872CB06427AF20733C" 
validation="SHA1"></machinekey>

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 theMachine.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 theAutoGenerate option too to enable/disable autogeneration of key during runtime.


1、pages节点修改:

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

2、生成 machinekey:

http://aspnetresources.com/tools/keycreator.aspx

把生成的machinekey 加入 webconfig对应的节点。

比如:<machineKey validationKey="3FF1E929BC0534950B0920A7B59FA698BD02DFE8" decryptionKey="280450BB36319B474C996B506A95AEDF9B51211B1D2B7A77" decryption="3DES"  validation="SHA1"/>


2014-07-10添加:实践心得

3、如果多个子站点之间共享登陆帐号,得在不同的web.config里设置同一个machineKey。



版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。

相关文章
ASP.NET Core : 一. 概述
ASP.NET Core : 一. 概述
69 0
ASP.NET MVC (五、HttpClient接口解析)(6)
ASP.NET MVC (五、HttpClient接口解析)(6)
122 0
ASP.NET MVC (五、HttpClient接口解析)(5)
ASP.NET MVC (五、HttpClient接口解析)(5)
77 0
ASP.NET MVC (五、HttpClient接口解析)(4)
ASP.NET MVC (五、HttpClient接口解析)(4)
31 0
ASP.NET MVC (五、HttpClient接口解析)(3)
ASP.NET MVC (五、HttpClient接口解析)(3)
72 0
ASP.NET MVC (五、HttpClient接口解析)(2)
ASP.NET MVC (五、HttpClient接口解析)(2)
74 0
ASP.NET MVC (五、HttpClient接口解析)(1)
ASP.NET MVC (五、HttpClient接口解析)(1)
48 0
ASP.NET MVC (四、ASP.NET Web API应用程序与跨域操作)(7)
ASP.NET MVC (四、ASP.NET Web API应用程序与跨域操作)(7)
62 0
ASP.NET MVC (四、ASP.NET Web API应用程序与跨域操作)(6)
ASP.NET MVC (四、ASP.NET Web API应用程序与跨域操作)(6)
53 0
+关注
真爱无限
主要技术.net sqlserver mysql。
文章
问答
文章排行榜
最热
最新
相关电子书
更多
低代码开发师(初级)实战教程
立即下载
阿里巴巴DevOps 最佳实践手册
立即下载
冬季实战营第三期:MySQL数据库进阶实战
立即下载