我们有 ASP.NET 4.0 网络正在 Windows Server 2008 R2 (安装在Amazon ec2实例上)。我们的用户时不时会收到如下错误:
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.
Source: System.Web
StackTrace:
at System.Web.UI.ViewStateException.ThrowMacValidationError(Exception inner, String persistedState)
at System.Web.UI.ObjectStateFormatter.Deserialize(String inputString)
at System.Web.UI.ScriptManager.LoadHistoryState(String serverState)
at System.Web.UI.ScriptManager.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument)
at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
at System.Web.UI.Page.ProcessRequest(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
at System.Web.UI.Page.ProcessRequest()
at System.Web.UI.Page.ProcessRequest(HttpContext context)
at System.Web.HttpApplication.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()
at System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously)
TargetSite: Void ThrowMacValidationError(System.Exception, System.String)
System.Web.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: Invalid viewstate.
Client IP: Some IP
Port: 49314
Referer: http://www.xxxx.com/xxxxxxx
Path: /xxxxxxx
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/33.0.1750.154 Safari/537.36
ViewState: /wEXAQUMVmVyUHJvZHVjdG9zBQxQVkQgQVNDIzQjMTCkifeeYf3AT+qOX2HL/wiZX7nF6E2sPjS/OhlzF+pfNg==
at System.Web.UI.ViewStateException.ThrowMacValidationError(Exception inner, String persistedState)
at System.Web.UI.ObjectStateFormatter.Deserialize(String inputString)
at System.Web.UI.ScriptManager.LoadHistoryState(String serverState)
at System.Web.UI.ScriptManager.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument)
at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
at System.Web.UI.Page.ProcessRequest(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
at System.Web.UI.Page.ProcessRequest()
at System.Web.UI.Page.ProcessRequest(HttpContext context)
at System.Web.HttpApplication.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()
at System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously)
我们已经找到了解决方案,但似乎没有任何效果。我们添加了一个机器密钥,也出现了错误。当我们移动到EC2时,这个错误开始出现。在我们自己的旧专用服务器上没有问题。
注意:设置enableEventValidation =“false”不是一个选项。
答案 0 :(得分:0)
这通常在您创建动态控件时发生,并且每次重新创建时都不会设置相同的ID(我希望每次使用Page_Init重新创建它们)。问题的另一个原因可能是当您修改服务器上的某些控件时(例如,清除控件容器),但此更改不会传递到客户端(即,它发生在部分回发期间)。下次页面被POST时,客户端将发送视图状态并控制服务器上不存在的值(例如,输入)。 因此,在页面中查找此类逻辑和/或尝试禁用AJAX以查看是否存在差异。
另一种选择是尽可能多地禁用ViewState,但很少需要它。
另一个选择是将ViewState存储在服务器而不是隐藏字段中。
答案 1 :(得分:0)
正如@rdmptn所说,这种情况背后有很多原因。 其中一个是应用程序池回收。 如果用户在网站上忙碌并且应用程序池被回收,那么服务器将找不到回发的viewstate / session并抛出错误。这通常发生在我们没有明确提供机器时。
所以解决方案是在web.config中提供机器密钥。 &LT; machinekey validationkey =&#34; xxx&#34; decryptionkey =&#34; XXX&#34;验证=&#34; SHA1&#34;解密=&#34; AES&#34; /&GT;
有关详细信息,请查看http://www.codeproject.com/Questions/464873/Validation-of-viewstate-MAC-failed
您可以从以下网址生成机器密钥
http://aspnetresources.com/tools/machineKey
并将该machinekey元素放在web.config
中