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



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

在我的情况下,这个错误似乎完全是随机的。我会好几个星期都看不到它,然后有一天我会登录我的网站并得到错误。刷新页面,再次登录,没有问题。在事件之间的这段时间里,web服务器没有发生任何特别的事情。

这是另一件事,错误显示"如果这个应用程序由Web场或集群托管…",但事实并非如此。这只是一个单一的WindowsServer2008。

所以,我知道一般的解决方案是在web.config文件中添加一组静态机器密钥,但为什么这似乎是随机发生的,为什么它会发生在我的站点上,而我的站点位于一台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

从您的日志中,我建议您检查一件事-您是否将用户的任何参数与Viewstate连接,如果用户登录/注销,则可以更改Viewstate-或由于任何其他原因而更改,例如会话已过期或更改。

此参数在保存的视图状态和当前视图状态之间可能不同,这可能会打断页面。

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

ViewStateUserKey = CurrentUserCookie;

或者这个:

ViewStateUserKey = User.Identity.Name

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

这是类似的:ViewStateUserKey+共享主机+ViewStateMac验证失败

以及一些参考:http://www.hanselman.com/blog/ViewStateUserKeyMakesViewStateMoreTamperresistant.aspx
http://msdn.microsoft.com/en-us/library/ms972969.aspx

随机错误是任何开发者的噩梦

最新更新