c# - 为什么我只*随机*在单个服务器环境中获得 "Validation of viewstate MAC failed"?

标签 c# asp.net exception viewstate machinekey

是的,关于这个特定错误有一百万个问题和线程,有许多记录完备的解决方案,我一定会尝试。但首先我想了解我的特定情况/环境的问题。

在我的情况下,这个错误似乎完全是随机的。我会在几周内看不到它,然后有一天我会登录我的网站并收到错误消息。刷新页面,重新登录,没有问题。在两次事件之间的这段时间里,Web 服务器没有发生任何特别的事情。

这是另一回事,错误显示“如果此应用程序由网络场或集群托管...”,但事实并非如此。它只是一个 Windows Server 2008。

所以,我知道一般的解决方案是在 web.config 文件中添加一组静态机器 key --- 但为什么这似乎是随机发生的,为什么它发生在我的网站上,它在一个单一的Windows Server 2008 机器?

我使用的是 ASP.NET 4,我的站点是一个在 IIS 上运行的 Web 应用程序。

错误信息:

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.

堆栈跟踪:

[ViewStateException: Invalid viewstate. 
    Client IP: ##::##:##:##:##
    Port: ##
    Referer: http://myserver/login.aspx
    Path: /login.aspx
    User-Agent: Opera/9.80 (Windows NT 6.1; WOW64; U; en) Presto/2.10.289 Version/12.01
    ViewState: /wEPDw...6OCX]

[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) +235
   System.Web.UI.ViewStateException.ThrowMacValidationError(Exception inner, String persistedState) +14
   System.Web.UI.ObjectStateFormatter.Deserialize(String inputString) +274
   System.Web.UI.ObjectStateFormatter.System.Web.UI.IStateFormatter.Deserialize(String serializedState) +4
   System.Web.UI.Util.DeserializeWithAssert(IStateFormatter formatter, String serializedState) +37
   System.Web.UI.HiddenFieldPageStatePersister.Load() +241
   System.Web.UI.Page.LoadPageStateFromPersistenceMedium() +106
   System.Web.UI.Page.LoadAllState() +43
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +8431
   System.Web.UI.Page.ProcessRequest(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +253
   System.Web.UI.Page.ProcessRequest() +78
   System.Web.UI.Page.ProcessRequestWithNoAssert(HttpContext context) +21
   System.Web.UI.Page.ProcessRequest(HttpContext context) +49
   ASP.login_aspx.ProcessRequest(HttpContext context) +4
   System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +100
   System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +75

最佳答案

从您的日志中,我建议您检查一件事 - 您是否将用户的任何参数与用户登录/注销时可以更改的 View 状态相关联? - 或更改出于任何其他原因,例如因为 session 已过期或更改。

此参数在保存的和当前的 View 状态之间可能不同,这可能会破坏页面。

例如,这段代码可以做到这一点:

ViewStateUserKey = CurrentUserCookie;

或者这个是这样的:

ViewStateUserKey = User.Identity.Name

另请阅读此答案:https://stackoverflow.com/a/2551810/159270

这是类似的: ViewStateUserKey + shared hosting + ViewStateMac validation failure

还有一些引用: http://www.hanselman.com/blog/ViewStateUserKeyMakesViewStateMoreTamperresistant.aspx
http://msdn.microsoft.com/en-us/library/ms972969.aspx

随机错误是任何开发人员的噩梦。

关于c# - 为什么我只*随机*在单个服务器环境中获得 "Validation of viewstate MAC failed"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12031756/

相关文章:

c# - 如何设置 SQL Server Compact 数据库的相对路径?

jquery - 先进的 ASP.NET 提高性能

c# - 检查 WebException 中的证书验证错误

c# - X = X++; 有什么区别?与 X++;?

Python从HTTPS aspx下载图像

asp.net 检查gridview中的复选框是否被选中

python - 如何处理列表推导中的异常?

java - 如何处理多层事件,Java

java - 延迟异常

c# - Cuda - OpenCL CPU 比 OpenCL 或 CUDA GPU 版本快 4 倍