web.config no relevant source lines

 

C# code
  
  
Server Error in ' / ' Application. -------------------------------------------------------------------------------- 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. Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. Exception Details: System.Web.HttpException: 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. Source Error: [No relevant source lines] Source File: c:/windows/Microsoft.NET/Framework/v2. 0.50727 /Temporary ASP.NET Files/root/916d9c69/503e44c4/App_Web_utcozqn_. 1 .cs Line: 0 Stack Trace: [HttpException ( 0x80004005 ): Unable to validate data.] System.Web.Configuration.MachineKeySection.GetDecodedData(Byte[] buf, Byte[] modifier, Int32 start, Int32 length, Int32 & dataLength) + 294 System.Web.UI.ObjectStateFormatter.Deserialize(String inputString) + 203 [ViewStateException: Invalid viewstate. Client IP: 123.113 . 122.102 Port: 7929 User - Agent: Mozilla / 4.0 (compatible; MSIE 6.0 ; Windows NT 5.2 ; SV1; .NET CLR 1.1 . 4322 ; .NET CLR 2.0 . 50727 ; .NET CLR 3.0 . 04506.648 ; .NET CLR 3.5 . 21022 ; CIBA) ASP.amina_aboutus_aspx.ProcessRequest(HttpContext context) in c:/windows/Microsoft.NET/Framework/v2. 0.50727 /Temporary ASP.NET Files/root/916d9c69/503e44c4/App_Web_utcozqn_. 1 .cs: 0 -------------------------------------------------------------------------------- Version Information: Microsoft .NET Framework Version: 2.0 . 50727.1433 ; ASP.NET Version: 2.0 . 50727.1433
源地址:http://bbs.powereasy.net/dispbbs.asp?BoardID=73&ID=397124
不知道对你有没有帮助


Validation of viewstate MAC failed异常的原因及解决方法
对于内容较多,功能较为复杂的页面,如本站的课程详细信息页面,偶尔会出现Validation of viewstate MAC failed的错误。
在异常的信息里,会说是在web farm或者集群环境下,machineKey的配置不一致导致的。而事实上,我们还并没有使用web场和集群,因此异常的提示信息显然是一种误导了。

事件日志里偶尔能发现这个错误,却一直找不到有效的解决方法。经过仔细研究分析之后,发现出现这个错误的概率还是很小的,还需要满足多个条件才行:
1.页面中使用了GridView, DetailsViews, FormView等采用内置数据绑定控件
2.就是这个页面的内容较多,在网速较慢的情况下需要较长的时间才能加载完
3.在页面还没有加载完成的情况下,点击了Postback性质的按钮。如果是get方式的url链接请求则不存在此问题

内在的深层次原因,是由于GridView等控件,为了避免暴露数据库中的字段名称,在默认的asp.net 2.0设置之下,会对GridView里用到的DataKeyNames使用加密方式存储在ViewState里,并在页尾 </form>标签标签之前写入一个隐藏域 <input type="hidden" name="__VIEWSTATEENCRYPTED" id="__VIEWSTATEENCRYPTED" value="" />用来表明ViewState是使用加密存储的。
由于这个隐藏域在页尾,因此在前面所述为页面尚未完全加载的情况下就点击Post方式的按钮,就会导致在服务器端处理反序列化viewstate时,因为找不到这个隐藏域而认为是非法的ViewState,而抛出这个异常。

解决方法就是禁止ViewState的加密,不利之处是会降低网站的安全性。
如果要禁用整个站点或者模块的ViewState加密,只需在web.config里作如下设置即可:
lt;pages enableEventValidation="false" viewStateEncryptionMode ="Never" />
当然也可以只对有出现这个问题页面的 <@page作enableEventValidation="false" viewStateEncryptionMode ="Never" 设置,这样的话只会在这个页面的ViewState不作加密,不会影响到整个站点所有页面的安全性。

微软已经确认这是一个Bug,详细地讨论也可以参考asp.net官方站点 http://forums.asp.net/1/1173230/ShowThread.aspx
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值