今天在调试Atlas时遇到错误:
验证视图MAC失败。如果此引用程序由网络场或群集承载,请确保
发生错误的环境:
ASP.NET 2.0,使用Atlas的UpdatePanel,在UpdatePanel中动态加载用户控件,以达到动态更新页面的效果。其中有一个用户控件中使用了GridView。当动态切换页面时,出现上述错误。
问题分析:
经过一番搜索,找到以下的文章:
http://aspadvice.com/blogs/joteke/archive/2006/02/02/15011.aspx
http://forums.asp.net/1173230/ShowPost.aspx
分析后找到了问题的根源。首先,文章中提到,如果用GridView,并且指定了DataKeyNames属性,则出于安全的理由(因为 DataKeyNames指定的字段代表数据的主键,且该主键值需要保存在视图状态中发送到客户端,用户如果篡改主键值,会导致安全问题), GridView会要求加密视图状态。为此会自动在页面表单之前添加一个 。
然而,Atlas的UpdatePanel要求放置在
内部,也就是 之前。这就意味着添加的隐藏input控件没有被放置在UpdatePanel内,而是放置在UpdatePanel和之间。当UpdatePanel更新时,UpdatePanel内部的控件被提交到服务器进行处理(Patrial Rendering),而整个页面并没有被提交。也就是说隐藏的input控件没有随着一起提交。因此服务器并不知道提交的ViewState被加密了,从而导致MAC验证错误。
解决方法:
通过在Web.config里边添加
可以解决该问题。
If you get this Exception
[HttpException (0x80004005): Validation of viewstate MAC failed. If this application is hosted by a Web Farm or cluster, ensure that
and
If following preconditions are true and you click a postbacking control/link while the Page hasn't loaded completely, you might get the "Validation of ViewState MAC failed" exception. In this case be sure to check following post on ASP.NET Forums where this has been discussed quite thoroughly : http://forums.asp.net/1173230/ShowPost.aspx
It appears because GridView using DataKeyNames requires ViewState to be encrypted. And when ViewState is encrypted, Page adds field just before closing of the