gpt4 book ai didi

.net - 如何解决 ASP.Net MVC 应用程序中 iisreset 后发生的 AntiForgeryToken 异常?

转载 作者:行者123 更新时间:2023-12-03 07:08:40 24 4
gpt4 key购买 nike

我在 ASP.Net MVC 中遇到 AntiForgeryToken 问题。如果我在我的 Web 服务器上执行 iisreset 并且用户继续其 session ,他们会跳到登录页面。并不可怕,但是 AntiForgery token 会爆炸,再次启动的唯一方法就是炸掉浏览器上的 cookie。

在版本 1 的测试版中,当我读回 cookie 时,它​​常常会出错,所以我过去常常在请求验证 token 之前清理它,但在发布时这个问题得到了修复。

现在我想我会回滚到修复测试版问题的代码,但我忍不住认为我错过了一些东西。有没有更简单的解决方案,我应该放弃他们的助手并从头开始创建一个新的吗?我感觉很多问题是因为它与旧的 ASP.Net 管道紧密相连,并试图将其拼凑起来,去做一些原本不是设计要做的事情。

我查看了 ASP.Net MVC 2 RC 的源代码,看起来代码没有太大变化,所以虽然我还没有尝试过,但我认为那里没有任何答案.

这是异常堆栈跟踪的相关部分。

编辑:我刚刚意识到我没有提到这只是尝试在 GET 请求上插入 token 。这不是您启动 POST 时发生的验证。

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 useValidationSymAlgo)
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设置为“自动生成”,那么您的验证 token 等将无法在应用程序重新启动后继续存在 - ASP.NET 将在启动时生成新 key ,然后将无法正确解密 token 。

如果您经常看到这种情况,我建议:

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

1 实现此目的的最佳方法是使用负载平衡应用程序,这将要求您设置静态 MachineKey。另一种选择是通过在网站的根目录中放置一个名为 app_offline.htm 的文件来关闭网站,这将使网站脱机并显示您的消息 - 至少用户会期望一切正常错了。

关于.net - 如何解决 ASP.Net MVC 应用程序中 iisreset 后发生的 AntiForgeryToken 异常?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2206595/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com