我对ASP中的AntiForgeryToken有问题.Net MVC.如果我在我的网络服务器上做了一个iisreset,用户继续他们的会话,他们就会跳转到登录页面.这并不可怕,但随后,防暴令牌爆炸,唯一的办法是再次启动浏览器上的cookie.

在版本1的测试版中,当我读回cookie时会出错,所以我在请求验证令牌之前会擦除它,但当它发布时,这个问题就被修复了.

现在,我想我会回到我的代码,修复测试版的问题,但我不能不认为我错过了什么.有没有更简单的解决方案,见鬼,我应该放弃他们的助手,从头开始创建一个新的吗?我觉得很多问题都是因为它与旧的ASP紧密相连.Net管道,并试图让它做一些它并不是真正设计来做的事情.

我查看了ASP的源代码.Net MVC 2 RC,代码似乎没有太大变化,所以虽然我还没有try 过,但我认为没有任何答案.

以下是异常的堆栈跟踪的相关部分.

Edit:我刚刚意识到我没有提到这只是试图在GET请求中插入令牌.这不是当您开始发布帖子时发生的验证.

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 <machineKey> 
configuration specifies the same validationKey and validation algorithm. 
AutoGenerate cannot be used in a cluster.
---> System.Web.UI.ViewStateException: Invalid viewstate. 
  Client IP: 127.0.0.1
  Port: 4991
  User-Agent: scrubbed
  ViewState: scrubbed
  Referer: blah
  Path: /oursite/Account/Login
---> System.Security.Cryptography.CryptographicException: Padding is invalid and
cannot be removed.
at System.Security.Cryptography.RijndaelManagedTransform.DecryptData(Byte[] inputBuffer, Int32 inputOffset, Int32 inputCount, Byte[]& outputBuffer, Int32 outputOffset, PaddingMode paddingMode, Boolean fLast)
at System.Security.Cryptography.RijndaelManagedTransform.TransformFinalBlock(Byte[] inputBuffer, Int32 inputOffset, Int32 inputCount)
at System.Security.Cryptography.CryptoStream.FlushFinalBlock()
at System.Web.Configuration.MachineKeySection.EncryptOrDecryptData(Boolean fEncrypt, Byte[] buf, Byte[] modifier, Int32 start, Int32 length, IVType ivType, Boolean useValidationSym算法)
at System.Web.UI.ObjectStateFormatter.Deserialize(String inputString)
--- End of inner exception stack trace ---
--- End of inner exception stack trace ---
at System.Web.UI.ViewStateException.ThrowError(Exception inner, String persistedState, String errorPageMessage, Boolean macValidationError)
at System.Web.UI.ViewStateException.ThrowMacValidationError(Exception inner, String persistedState)
at System.Web.UI.ObjectStateFormatter.Deserialize(String inputString)
at System.Web.UI.ObjectStateFormatter.System.Web.UI.IStateFormatter.Deserialize(String serializedState)
at System.Web.Mvc.AntiForgeryDataSerializer.Deserialize(String serializedToken)
--- End of inner exception stack trace ---
at System.Web.Mvc.AntiForgeryDataSerializer.Deserialize(String serializedToken)
at System.Web.Mvc.HtmlHelper.GetAntiForgeryTokenAndSetCookie(String salt, String domain, String path)
at System.Web.Mvc.HtmlHelper.AntiForgeryToken(String salt, String domain, String path)

推荐答案

如果您的MachineKey设置为AutoGenerate,那么您的验证令牌等将在应用程序重新启动后失效-ASP.NET将在其启动时生成一个新密钥,然后将无法正确解密令牌.

如果你经常看到这一点,我建议:

  1. 配置静态MachineKey(您应该能够在应用程序级别执行此操作),有关更多信息,请参阅"How to: Configure a MachineKey"
  2. try 在使用站点时不执行IIS重置1

1实现这一点的最佳方法是使用loadbalanced应用程序,这将要求您设置静态MachineKey.另一种 Select 是在网站的根目录下放置一个名为app_offline.htm的文件来关闭网站,这将使网站离线并显示您的消息——至少用户会认为会出问题.

Asp.net相关问答推荐

Windows 命令行中的/p:是什么意思

检测 ASP.NET 中的内存泄漏

将 Global.asax 迁移到 Startup.cs

业务线应用程序:F# 会让我的生活变得轻松吗?

ASP.NET Response.Redirect 使用 302 而不是 301

如何使用 SmtpClient.SendAsync 发送带有附件的邮箱?

ASP.NET MVC 中的 ASP.NET AJAX 与 jQuery

如何更改 .ASPX 自动格式化设置 (Visual Studio)

如何将 Web 应用程序项目转换为类库项目

与将 Web 应用程序保存在一个默认应用程序池中相比,拥有专用应用程序池的优缺点

在 asp.net 中调整图像大小而不会丢失图像质量

如何在 ASP.NET 中仅在调试模式下执行代码

如何使用 app_GlobalResource 或 app_LocalResource?

带有完整内存错误的 WCF 服务(内存门判断失败,因为可用内存) - 如何解决

使下拉列表项不可 Select

使用 JObject 所需的库名称是什么?

在 ASP.NET 中实现 404 的最佳方法

ASP.NET Core 中的 NuGet 包位置在哪里?

删除 HTML 或 ASPX 扩展

如何在服务器控件属性中使用 ASP.NET <%= 标签?