【问题标题】:ASP.Net MVC - Viewstate validation failed on page with no viewstate (or server side controls)ASP.Net MVC - 没有视图状态(或服务器端控件)的页面上的视图状态验证失败
【发布时间】:2009-12-31 17:27:54
【问题描述】:

对一个真正令人沮丧的错误的有趣看法。我有一个没有 runat=server 控件的 MVC 页面,只有一个表单,但在阅读我的错误日志时,我仍然在页面上收到“Validation of viewstate MAC failed”异常。

这里是抛出的异常:

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: 10.XXX.XXX.XXX Port: 33791 
User-Agent: Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.9.1.5) Gecko/20091102 Firefox/3.5.5 (.NET CLR 3.5.30729) 
ViewState: /wEPDwUKMTc2MzIxNTAwOWRkLTq4ngVrnkwCLjQCKKiLdjGVFbs=

这是完整的堆栈:

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: 10.XXX.XXX.XXX
    Port: 33791
    User-Agent: Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.9.1.5) Gecko/20091102 Firefox/3.5.5 (.NET CLR 3.5.30729)
    ViewState: /wEPDwUKMTc2MzIxNTAwOWRkLTq4ngVrnkwCLjQCKKiLdjGVFbs=
    Referer: http://www.shelfari.com/search/combine?Keywords=turn%20of%20the%20screw
    Path: /search/Combine ---> System.Web.HttpException: Unable to validate data.
   at System.Web.Configuration.MachineKeySection.GetDecodedData(Byte[] buf, Byte[] modifier, Int32 start, Int32 length, Int32& dataLength)
   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.ObjectStateFormatter.Deserialize(String inputString)
   at System.Web.UI.Util.DeserializeWithAssert(IStateFormatter formatter, String serializedState)
   at System.Web.UI.HiddenFieldPageStatePersister.Load()
   at System.Web.UI.Page.LoadPageStateFromPersistenceMedium()
   at System.Web.UI.Page.LoadAllState()
   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 ASP.views_search_bookcombined_aspx.ProcessRequest(HttpContext context)
   at System.Web.Mvc.ViewPage.RenderView(ViewContext viewContext)
   at System.Web.Mvc.ViewResultBase.ExecuteResult(ControllerContext context)
   at System.Web.Mvc.ControllerActionInvoker.<>c__DisplayClass11.<InvokeActionResultWithFilters>b__e()
   at System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilter(IResultFilter filter, ResultExecutingContext preContext, Func`1 continuation)
   at System.Web.Mvc.ControllerActionInvoker.InvokeActionResultWithFilters(ControllerContext controllerContext, IList`1 filters, ActionResult actionResult)
   at System.Web.Mvc.ControllerActionInvoker.InvokeAction(ControllerContext controllerContext, String actionName)
   at System.Web.Mvc.Controller.ExecuteCore()
   at System.Web.Mvc.MvcHandler.ProcessRequest(HttpContextBase httpContext)
   at System.Web.HttpApplication.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()
   at System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously)

尚不清楚为什么页面上会出现视图状态 - 我一直无法重现该错误,但它偶尔会出现在日志中。收到错误的用户往往是受信任的个人,不太可能手动操作视图状态。

生产环境是一个 webfarm,指定了 machineKey 配置,在所有机器上使用 validationKey 和验证算法。

有什么想法吗?

【问题讨论】:

  • 如果您能提供完整的堆栈跟踪信息将会很有帮助。谢谢!
  • Levi,我已经内联添加了完整的堆栈跟踪。抱歉耽搁了,“忘记”了手头的问题……
  • 只有当请求(表单或查询字符串)包含 __VIEWSTATE 键时,才应点击该代码路径。发生此异常时,是否可以准确查看客户端向服务器发送的内容?例如,您能否通过异常过滤器或 Application_Error 捕获这些异常并转储 QueryString 和 Form 的全部内容?
  • Levi,我必须更新服务器日志以获取更多表单内容。我可能需要一两天的时间来获得足够的数据来工作……所以我会在星期一回来报告。
  • 自从我添加了额外的日志记录以来,此问题尚未重现...目前不知道该说什么。感谢所有提供帮助的人。

标签: asp.net asp.net-mvc viewstate


【解决方案1】:

我也见过这个。我认为原因是搜索引擎访问 - 搜索引擎访问页面的方式必须缺少某些东西。至少当我添加 robots.txt 文件并告诉搜索引擎不要抓取我的登录页面(使用表单和防伪令牌)时,问题就消失了。

【讨论】:

  • 很有趣,但这里不太可能出现这种情况。有问题的页面仅对经过身份验证的用户可见(排除蜘蛛),并且不是通常书签或缓存的地方 - 它是允许编辑搜索结果的页面(主要排除缓存问题)
  • 可能是antiforgery cookie超时,然后表单被提交的结果。这可能与搜索引擎不随请求一起提交防伪 cookie 是一致的。
  • 据我了解,AntiForgertyToken 需要手动呈现到相关页面 - 我没有为这个特定页面执行此操作。值得注意的是,这个问题发生在经过身份验证的用户、管理员身上,实际上......
【解决方案2】:

您可以尝试使用位于 Views 目录(部分页面)中的 web.config 在所有视图中禁用验证视图状态。

您可以禁用所有其他 WebForms “futures”,例如 AutoEventWireup :)

【讨论】:

  • 这可能行得通,但对我来说并不实用。我运行一个混合网站,其中我的主模板是一个 MVC 视图(在视图目录下),但也有依赖于视图状态的页面。在测试中,禁用视图目录上的视图状态似乎确实破坏了功能,即使这些页面位于视图目录之外。
猜你喜欢
  • 1970-01-01
  • 2011-08-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-09-19
  • 2010-09-24
相关资源
最近更新 更多