【问题标题】:Html.AntiForgeryToken() causeing errors after upgrading to .NET 3.5 SP1Html.AntiForgeryToken() 升级到 .NET 3.5 SP1 后导致错误
【发布时间】:2010-10-06 21:49:37
【问题描述】:

我刚刚更新到 .NET 3.5 SP1,而我曾经工作的 ASP.NET MVC 页面现在已经停止工作。

当尝试加载页面时,我得到以下 YSOD

[CryptographicException: Padding is invalid and cannot be removed.]
   System.Security.Cryptography.RijndaelManagedTransform.DecryptData(Byte[] inputBuffer, Int32 inputOffset, Int32 inputCount, Byte[]& outputBuffer, Int32 outputOffset, PaddingMode paddingMode, Boolean fLast) +7596702
   System.Security.Cryptography.RijndaelManagedTransform.TransformFinalBlock(Byte[] inputBuffer, Int32 inputOffset, Int32 inputCount) +208
   System.Security.Cryptography.CryptoStream.FlushFinalBlock() +33
   System.Web.Configuration.MachineKeySection.EncryptOrDecryptData(Boolean fEncrypt, Byte[] buf, Byte[] modifier, Int32 start, Int32 length, IVType ivType, Boolean useValidationSymAlgo) +225
   System.Web.UI.ObjectStateFormatter.Deserialize(String inputString) +195

[ViewStateException: Invalid viewstate. 
    Client IP: 127.0.0.1
    Port: 
    User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-GB; rv:1.9.0.6) Gecko/2009011913 Firefox/3.0.6 (.NET CLR 3.5.30729)
    ViewState: hC6BC8KsuD/yoy2iG74bUZ8TYhGfuDDeIjh9fg/L18yr/E+1Nk/pjS5gyn9O+2jY
    Referer: http://localhost:1092/admin/product
    Path: /admin.aspx/product/edit/4193]

[HttpException (0x80004005): 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.]
   System.Web.UI.ViewStateException.ThrowError(Exception inner, String persistedState, String errorPageMessage, Boolean macValidationError) +106
   System.Web.UI.ViewStateException.ThrowMacValidationError(Exception inner, String persistedState) +14
   System.Web.UI.ObjectStateFormatter.Deserialize(String inputString) +242
   System.Web.UI.ObjectStateFormatter.System.Web.UI.IStateFormatter.Deserialize(String serializedState) +4
   System.Web.Mvc.AntiForgeryTokenSerializer.Deserialize(String serializedToken) +73

如果我删除该行

<%= Html.AntiForgeryToken() %>

一切都恢复正常了,有什么想法可能导致这种情况吗?如果这是一个 MVC 或 .NET 问题,我本来希望找到解决方案的运气更好,所以我猜这与我的配置有关。

我尝试重新安装 MVC 框架,看看是否是因为我在 SP1 之前安装,但我仍然遇到同样的问题。 Google 和 SO 搜索尚未得出任何确定的结论。

【问题讨论】:

    标签: asp.net-mvc .net-3.5 cryptography viewstate


    【解决方案1】:

    如果站点已部署并且您正在进行维护(包括 ASP.NET MVC 程序集更新),则不能选择清除浏览器缓存。这是我使用的解决方案:

    @Html.AntiForgeryTokenReset() @* use this instead*@
    

    这里是扩展方法

    public static MvcHtmlString AntiForgeryTokenReset(this HtmlHelper htmlHelper)
    {
        try
        {
            return htmlHelper.AntiForgeryToken();
        } catch (Exception ex)
        {
            var request = HttpContext.Current.Request;
            request.Cookies.Clear();
            return htmlHelper.AntiForgeryToken();
        }
    }
    

    【讨论】:

    • 有趣的解决方案。我想过使用它,但由于 cookie 是一个会话 cookie,当访问者关闭浏览器时它会消失。因为我已经在 web.config 中添加了机器密钥,所以在以后的更新中不会有问题。
    【解决方案2】:

    现在看来这个问题已经解决了。看看http://forums.asp.net/p/1388671/2960554.aspx

    【讨论】:

    • 这适用于本地开发,其中站点覆盖彼此的cookie,已修复。
    【解决方案3】:

    Doh,刚刚解决了。

    清除了我的浏览器缓存和 cookie,一切正常。

    【讨论】:

    • 谢谢!这真的帮助了我,快疯了。
    • 为我节省了 2 小时的研究时间。谢谢
    • 同样如此。为什么这可以解决问题?为什么首先会出现问题?
    • 天哪,毕竟我试过了,生成了一堆机器密钥,我所要做的就是清除 cookie!非常感谢!
    • 关闭浏览器也可以,因为它是使用的会话 cookie。在 web.config 中添加 machinekey 是长期的解决方案,否则每次重新启动站点时都会出现此问题。见stackoverflow.com/questions/1360078/…
    猜你喜欢
    • 2018-02-25
    • 1970-01-01
    • 1970-01-01
    • 2016-03-22
    • 2010-12-19
    • 2021-03-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多