MAC的ViewState的ASP.NET MVC验证失败 [英] ASP.NET MVC Validation of ViewState MAC failed

查看:834
本文介绍了MAC的ViewState的ASP.NET MVC验证失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

出版我的ASP.NET MVC Web应用程序的新的版本后,我经常看到在浏览该网站时,此异常抛出:

System.Web.Mvc.HttpAntiForgeryException:一个必需的防伪标记不提供或无效。 ---> System.Web.HttpException:视图状态MAC的验证失败。如果此应用程序由Web场或群集承载,确保配置指定了相同的validationKey和验证算法。自动生成不能在集群中使用。 ---> System.Web.UI.ViewStateException:无效的视图状态

这个异常会继续下去,直到我关闭Firefox出每个我在Web应用程序中访问页面上出现。重启火狐之后,该网站完美的作品。任何想法是怎么回事?

其他注意事项:


  1. 我没有使用任何ASP.NET Web控件(就在我的应用程序没有=服务器的实例)

  2. 如果我拿出的<%= Html.AntiForgeryToken%,从我的网页>,这个问题似乎消失


解决方案

在幕后,在MVC AntiForgeryToken属性使用加密将machineKey。如果没有指定在web.config一个的machineKey(见 href=\"http://msdn.microsoft.com/en-us/library/ms998288.aspx\">),一个是通过ASP.NET(完整描述)为您自动生成的。

如果ASP.NET应用程序重新启动(例如做一个的 IISRESET 的),浏览器cookie中的AntiForgeryToken仍然会使用旧机器密钥加密的,因此它以上述错误崩溃。

所以,你应该使用MVC时,始终指定你的web.config一个的machineKey例如。

 <结构>
    <&的System.Web GT;
        <的machineKey
            validationKey=\"21F090935F6E49C2C797F69BBAAD8402ABD2EE0B667A8B44EA7DD4374267A75D7AD972A119482D15A4127461DB1DC347C1A63AE5F1CCFAACFF1B72A7F0A281B\"
            decryptionKey =ABAA84D7EC4BB56D75D217CECFFB9628809BDB8BF91CFCD64568A145BE59719F
            验证=SHA1
            解密=AES
        />
    ...

After publishing a new build of my ASP.NET MVC web application, I often see this exception thrown when browsing to the site:

System.Web.Mvc.HttpAntiForgeryException: A required anti-forgery token was not supplied or was invalid. ---> System.Web.HttpException: Validation of viewstate MAC failed. If this application is hosted by a Web Farm or cluster, ensure that configuration specifies the same validationKey and validation algorithm. AutoGenerate cannot be used in a cluster. ---> System.Web.UI.ViewStateException: Invalid viewstate.

This exception will continue to occur on each page I visit in my web application until I close out of Firefox. After reopening Firefox, the site works perfectly. Any idea what's going on?

Additional notes:

  1. I am not using any ASP.NET web controls (there are no instances of runat="server" in my application)
  2. If I take out the <%= Html.AntiForgeryToken %> from my pages, this problem seems to go away

解决方案

Under the covers, the MVC AntiForgeryToken attribute uses the machinekey for encryption. If you don't specify a machinekey in the web.config (see here), one is automatically generated for you by ASP.NET (full description).

If the ASP.NET application is restarted (e.g. do an iisreset), the AntiForgeryToken within the browser cookie will still be encrypted with an old machine key, hence why it crashes with the above error.

So you should always specify a machinekey in your web.config when using MVC, e.g.

<configuration>
    <system.web>
        <machineKey  
            validationKey="21F090935F6E49C2C797F69BBAAD8402ABD2EE0B667A8B44EA7DD4374267A75D7AD972A119482D15A4127461DB1DC347C1A63AE5F1CCFAACFF1B72A7F0A281B"           
            decryptionKey="ABAA84D7EC4BB56D75D217CECFFB9628809BDB8BF91CFCD64568A145BE59719F"
            validation="SHA1"
            decryption="AES"
        />
    ...

这篇关于MAC的ViewState的ASP.NET MVC验证失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆