c# - .NET 应用程序中不稳定的无效 View 状态问题

标签 c# .net asp.net viewstate

我的 ASP.NET 事件查看器中似乎不时出现“无效 View 状态”申请。

其中大多数 (95%) 似乎都在引用 ScriptResource.axd(应用程序 使用 ASP.NET AJAX图书馆)。我无法删除 Ajax作为 Ajax 的库随处可见..

如何减少这些错误?我每天都会遇到 100-200 个错误,但我不知道如何修复它们!它们来自不同的浏览器、不同的 IP 和地理位置。

我很难重现这个问题,因为它几乎没有发生在我身上,它只突然发生在我身上 3-4 次。

错误:

Process information: 
    Process ID: 4004 
    Process name: w3wp.exe 
    Account name: NT AUTHORITY\NETWORK SERVICE 

Exception information: 
    Exception type: HttpException 
    Exception message: Invalid viewstate. 

Request information: 
    Request URL: http://domainnamehere/ScriptResource.axd?d=W1R6x9VzZ2C9SKnIkOmX9VRLhSjJ3nOF1GSQvPwKS3html 
    Request path: /ScriptResource.axd 
    User host address: 124.177.170.75 
    User:  
    Is authenticated: False 
    Authentication Type:  
    Thread account name: NT AUTHORITY\NETWORK SERVICE 

Thread information: 
    Thread ID: 1 
    Thread account name: NT AUTHORITY\NETWORK SERVICE 
    Is impersonating: False 
    Stack trace:    at System.Web.UI.Page.DecryptStringWithIV(String s, IVType ivType)
   at System.Web.UI.Page.DecryptString(String s)
   at System.Web.Handlers.ScriptResourceHandler.DecryptParameter(NameValueCollection queryString)
   at System.Web.Handlers.ScriptResourceHandler.ProcessRequestInternal(HttpResponse response, NameValueCollection queryString, VirtualFileReader fileReader)
   at System.Web.Handlers.ScriptResourceHandler.ProcessRequest(HttpContext context)
   at System.Web.Handlers.ScriptResourceHandler.System.Web.IHttpHandler.ProcessRequest(HttpContext context)
   at System.Web.HttpApplication.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()
   at System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously)


Custom event details: 

For more information, see Help and Support Center at http://go.microsoft.com/fwlink/events.asp.

我的 .NET 代码中时不时也会出现此错误,该错误同时发生,这可能是相关的:

Exception raised in GLOBAL.ASAX.Application_Error(): 'Padding is invalid and cannot be removed.' at System.Security.Cryptography.RijndaelManagedTransform.DecryptData(Byte[] inputBuffer, Int32 inputOffset, Int32 inputCount, Byte[]& outputBuffer, Int32 outputOffset, PaddingMode paddingMode, Boolean fLast)
   at System.Security.Cryptography.RijndaelManagedTransform.TransformFinalBlock(Byte[] inputBuffer, Int32 inputOffset, Int32 inputCount)
   at System.Security.Cryptography.CryptoStream.FlushFinalBlock()
   at System.Web.Configuration.MachineKeySection.EncryptOrDecryptData(Boolean fEncrypt, Byte[] buf, Byte[] modifier, Int32 start, Int32 length, IVType ivType, Boolean useValidationSymAlgo)
   at System.Web.UI.ObjectStateFormatter.Deserialize(String inputString)

最佳答案

这似乎与许多人遇到的 IE8 问题相同。似乎发生的情况是 IE8(在 IE8 渲染模式和 IE7 兼容模式下)会以某种方式从 HTML 文档的中间丢失 4096 个字节,并且这种丢失的数据会导致这种异常(您通常会在 ScriptResource 或 WebResource 调用中看到这种情况) .

这是微软关于这个问题的错误报告: https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=434997

还有很多关于这个问题的论坛、博客等帖子:


微软已回应此问题:

注意是 Internet Explorer 8 中的错误。Internet Explorer 团队一直在调查此问题。

影响:到目前为止,我们认为该问题对最终用户使用 Web 应用程序的体验没有影响;唯一的负面影响是 JavaScript 推测下载引擎发送的虚假/格式错误的请求。当解析器实际需要脚本时,将在那时适本地下载和使用它。

情况:虚假请求似乎仅在某些时间情况下发生,仅当包含带有 CHARSET 指令的 Content-Type 的 META HTTP-EQUIV 标记出现在文档中时,并且仅当JavaScript SRC URL 跨越 HTTP 响应正文的第 4096 个字节。

解决方法:因此,我们目前认为可以通过使用 HTTP Content-Type header 声明页面的 CHARSET 而不是在页面中指定它来缓解此问题。

所以,而不是把

<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=utf-8">

在您的 head 标签中,发送以下 HTTP 响应 header :

Content-Type: text/html; charset=utf-8

请注意,在 HTTP header 中指定字符集会提高所有浏览器的性能,因为浏览器的解析器不需要在遇到字符集声明时从头开始重新解析。此外,使用 HTTP header 有助于缓解某些 XSS 攻击向量。

注意:有报告称,当 META HTTP-EQUIV 不在页面上时,此问题仍然会发生。当我们进行更多调查时,我们将更新此评论。

Microsoft 于 2009 年 6 月 30 日下午 12:25 发布。

编辑: 我仍然偶尔会看到这个异常,但这个错误被报告为已修复: Link

关于c# - .NET 应用程序中不稳定的无效 View 状态问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/728513/

相关文章:

c# - 如何在 VS2015 的断点条件表达式中使用 Environment.StackTrace

c# - RhinoMocks 中的 SetupResult 和 Stub 有区别吗?

c# - PadLeft 不起作用

c# - 写入json重构代码

c# - JavaScript 哈希和等效的 .NET 算法

.net - 将字符串参数发送到 WCF 服务会导致反序列化错误

.net - 打印枚举时的 ToString 异常

c# - 解析字符串包含一天中的时间

asp.net - Microsoft Azure 中 ASP.NET Web App 的访问性能计数器

javascript - 如何禁用客户端点击链接按钮或图像按钮