关于asp.net:System.Web.UI.ViewStateException,无效的viewstate

System.Web.UI.ViewStateException ,Invalid viewstate

我的网站每天都会抛出以下异常,我找不到解决方案。我很少在网络上做研究,这就是我目前所做的。将下面的选项添加到ASPX并不能解决问题。

ValidateRequest="false" EnableEventValidation="false"
ViewStateEncryptionMode="Never" EnableViewStateMac="false"

另外,我的引用URL是正确的URL,我不知道它如何使用两个(相同)参数进行重定向。在我的推荐页面中没有这样的链接。请帮忙。

错误日志

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
URL : http://www.abcd.com/company-details.aspx?com=asia-pacific-pte-ltd&com=asia-pacific-pte-ltd

referral URL : http://www.abcd.com/companies/asia-pacific-pte-ltd/

Inner Exception Type: System.Web.UI.ViewStateException

Inner Exception: Invalid viewstate. Client IP: 192.162.19.193 Port: 1966 User-Agent: Opera/9.80 (Windows NT 6.1; WOW64; MRA 6.0 (build 6001)) Presto/2.12.388 Version/12.11 ViewState: /wEPDwULLTEyMTAyMTY3NDAPZBYCAgMPZBYOZg8QDxYGHg1EYXRhVGV4dEZpZWxkBQdDb3VudHJ5Hg5EYXRhVmFsdWVGaWVsZAUNQ2xlYW5fQ291bnRyeR4LXyFEYXRhQm91bmRnZBAVCgNBbGwFQ2hpbmEJSG9uZyBLb25nBUluZGlhCUluZG9uZXNpYQhNYWxheXNpYQtQaGlsaXBwaW5lcwlTaW5nYXBvcmUIVGhhaWxhbmQHVmlldG5hbRUKA0FsbAVjaGluYQlob25nLWtvbmcFaW5kaWEJaW5kb25lc2lhCG1hbGF5c2lhC3BoaWxpcHBpbmVzCXNpbmdhcG9yZQh0aGFpbGFuZAd2aWV0bmFtFCsDCmdnZ2dnZ2dnZ2dkZAIBDxAPFgYfAAUISW5kdXN0cnkfAQUOQ2xlYW5fSW5kdXN0cnkfAmdkEBXFAgNBbGwSQWR2YW5jZWQgTWF0ZXJpYWxzC0FkdmVydGlzaW5nCkFlc3RoZXRpY3MLQWdyaWN1bHR1cmUkQWdyaWN1bHR1cmUgLSBBZ3JpY3VsdHVyYWwgTWFjaGluZXJ5G0FncmljdWx0dXJlICYgRm9vZCBQcm9kdWN0cxBBaXItY29uZGl0aW9uaW5nHUFpci1maWx0cmF0aW9uICYgUHVyaWZpY2F0aW9uB0FpcnBvcnQJQWx1bWluaXVtFEFuYWx5dGljYWwgRXF1aXBtZW50GkFuYWx5dGljYWwgSW5zdHJ1bWVudGF0aW9uFUFuYWx5dGljYWwgVGVjaG5vbG9neRZBbmQgSW50ZXJpb3IgRGVzaWduZXJzEUFuaW1hbCBQcm9kdWN0aW9uCEFudGlib2R5KUFwc...

Inner Source:

Exception Type: System.Web.HttpException

Exception: The state information is invalid for this page and might be corrupted.

Stack Trace: at System.Web.UI.ViewStateException.ThrowError(Exception inner, String persistedState, String errorPageMessage, Boolean macValidationError) 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.exhibition_details_aspx.ProcessRequest(HttpContext context) in c:\Windows\Microsoft.NET\Framework64\v2.0.50727\Temporary ASP.NET Files
oot\0f461847\442b0502\App_Web_bfjqxdef.20.cs:line 0 at System.Web.HttpApplication.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() at System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously)

更新我已经通过从下面的链接压缩视图状态修复了错误。http://www.codeproject.com/articles/14733/viewstate-compression

并通过将原始方法替换为base-64 char数组错误,修复了无效的长度。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
protected override object LoadPageStateFromPersistenceMedium()
    {
        string viewState = Request.Form["__VSTATE"];
        viewState = viewState.Replace("","+");

        int mod4 = viewState.Length % 4;
        if (mod4 > 0)
        {
            viewState += new string('=', 4 - mod4);
        }
        byte[] bytes = Convert.FromBase64String(viewState);
        bytes = Compressor.Decompress(bytes);
        LosFormatter formatter = new LosFormatter();
        return formatter.Deserialize(Convert.ToBase64String(bytes));
    }

感谢大家的帮助:)


很抱歉,您的站点没有任何内容,您需要尽快重新启用事件验证。

你所看到的是试图通过用你的viewstate发送许多测试号来破解你的站点,试图找到你的哈希键。

你给的IP在过去几天里有一大串的活动。

现在,另一种可能导致此错误的方法是破坏视图状态。如果视图状态太大,可以压缩并拆分它。您还可以禁用所有不需要的控件。此外,您还可以添加一个日志,从内部查看基本页上右侧的内容。


是的,我也喜欢

这里在VBNET中

压缩程序

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
Imports System.IO
Imports System.IO.Compression
Public Class Compressor

Public Shared Function Compress(ByVal data() As Byte) As Byte()
    Dim output As MemoryStream = New MemoryStream
    Dim gzip As GZipStream = New GZipStream(output, CompressionMode.Compress, True)
    gzip.Write(data, 0, data.Length)
    gzip.Close()
    Return output.ToArray
End Function

Public Shared Function Decompress(ByVal data() As Byte) As Byte()
    Dim input As MemoryStream = New MemoryStream
    input.Write(data, 0, data.Length)
    input.Position = 0
    Dim gzip As GZipStream = New GZipStream(input, CompressionMode.Decompress, True)
    Dim output As MemoryStream = New MemoryStream
    Dim buff() As Byte = New Byte((64) - 1) {}
    Dim read As Integer = -1
    read = gzip.Read(buff, 0, buff.Length)

    While (read > 0)
        output.Write(buff, 0, read)
        read = gzip.Read(buff, 0, buff.Length)

    End While

    gzip.Close()
    Return output.ToArray
 End Function
End Class

然后将其粘贴到default.aspx中它需要

导入system.io

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
  Protected Overrides Function LoadPageStateFromPersistenceMedium() As Object
    Dim viewState As String = Request.Form("__VSTATE")
    Dim bytes() As Byte = Convert.FromBase64String(viewState)
    bytes = Compressor.Decompress(bytes)
    Dim formatter As LosFormatter = New LosFormatter
    Return formatter.Deserialize(Convert.ToBase64String(bytes))
End Function

Protected Overrides Sub SavePageStateToPersistenceMedium(ByVal viewState As Object)
    Dim formatter As LosFormatter = New LosFormatter
    Dim writer As StringWriter = New StringWriter
    formatter.Serialize(writer, viewState)
    Dim viewStateString As String = writer.ToString
    Dim bytes() As Byte = Convert.FromBase64String(viewStateString)
    bytes = Compressor.Compress(bytes)
    ClientScript.RegisterHiddenField("__VSTATE", Convert.ToBase64String(bytes))
End Sub