2

我们遇到了以下情况:

  • 我们有一个在 .Net 2.0 框架上运行的 Web 应用程序。
  • 客户有一个在 .Net 4.5 框架上运行的 Web 应用程序。

当他们将数据从他们的一个页面发布到我们的页面时,我们会遇到系统错误。为了验证这个问题,我在同一台服务器上部署了两个 Web 应用程序,并尝试将表单从 webapp 1(在 4.5 上)发布到 webapp 2(在 2.0 上)。我得到以下错误。

[ViewStateException: Invalid viewstate. 
Client IP: 127.0.0.1
Port: 63153
User-Agent: Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0)
ViewState: /wEPDwUKLTM1MTA1NjA1MmRk6CPd6yG4r8HEbqBZi3i4jiLLnZotIlX7+6zAZaceaGY=
Referer: http://localhost/test4/default.aspx
Path: /test2/default.aspx]

[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) +148
System.Web.UI.ObjectStateFormatter.Deserialize(String inputString) +11065601
System.Web.UI.Util.DeserializeWithAssert(IStateFormatter formatter, String serializedState) +59
System.Web.UI.HiddenFieldPageStatePersister.Load() +11065704
System.Web.UI.Page.LoadPageStateFromPersistenceMedium() +11150648
System.Web.UI.Page.LoadAllState() +46
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +11146167
System.Web.UI.Page.ProcessRequest(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +11145706
System.Web.UI.Page.ProcessRequest() +91
System.Web.UI.Page.ProcessRequest(HttpContext context) +240
ASP.default_aspx.ProcessRequest(HttpContext context) in     c:\Windows\Microsoft.NET\Framework64\v2.0.50727\Temporary ASP.NET     Files\test2\b4717a74\d7ba8639\App_Web_ha1suqrp.0.cs:0
System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +599
System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +171

我尝试了以下方法:

  • 页面标记中的 EnableViewState = false。但是.Net 仍然添加了一个 _VIEWSTATE 隐藏变量。

  • 添加了一个 Javascript 函数来将 _VIEWSTATE 隐藏变量设置为空。这解决了这个问题。

虽然第二个工作解决方案解决了这个问题,但我正在寻找更好的方法来解决这类问题。

4

1 回答 1

1

EnableViewState = false仅关闭 ViewState,但不关闭 ASP.NET 服务器控件的控件状态,这就是为什么您仍然拥有隐藏的 VIEWSTATE 控件的原因。

ViewState 在服务器上是散列编码的,因此您应该使用相同的密钥来生成散列。您<machinekey>在 machine.config 文件中将两个应用程序/服务器上的值设置为相同的值。默认值为AutoGenerate,这意味着每个服务器都有自己的机器密钥。您可以在此处找到更多信息

作为替代方案,您可以通过设置为 false 来关闭散列EnableViewstateMac(基本上是您的错误消息所说的)。通过这样做,用户可以操纵您的 ViewState。

于 2013-11-12T20:23:10.763 回答