Uncategorized

ViewState validation troubles?

If you work with web sites in a complex/multi server environment you might be familiar with this error and as the error message itself suggest, the first thing to check if the machineKey value which must be the same across all of the involved server.

Anyway every now and then it happens that you might get this same error even if you are not in a NBL/Cluter environment (Tess and Tom have blogged about this here, here and here) or if you are absolutely sure you have properly configured the machineKey value. So what else can lease you to this state?

If you want some quick  background you have a look at my post here; basically we have the following (very raw) steps:

    • Begin processing the new request
    • Load the “old” state (ViewState and ControlState) received with the posted values
    • Process the posted data (the actual form values) and raise the “change” events to change control state and do the real page work
    • Save the new state (again, ViewState and ControlState)
    • Send the result back to the client

It is important to note that if you dynamically add controls at runtime on your page (i.e. you do not only rely on the page markup to create your layout) it is vital that the controls are added always in the same order or the viewstate validation will fail and you’ll get the error.

Some debugging

We got a dump of the problem; unfortunately in SOS for .NET 2.0 we miss some commands we have in SOS for .NET 1.1, but even if with a bit more typing and digging we can get the same results. The next logical step is to have a look at the exceptions on the managed heap:

0:033> !dumpheap -type Exception -stat
------------------------------
Heap 0
total 2 objects
------------------------------
Heap 1
total 4 objects
------------------------------
Heap 2
total 0 objects
------------------------------
Heap 3
total 7 objects
------------------------------
Heap 4
total 9 objects
------------------------------
Heap 5
total 0 objects
------------------------------
Heap 6
total 0 objects
------------------------------
Heap 7
total 4 objects
------------------------------
total 26 objects
Statistics:
      MT    Count    TotalSize Class Name
79103ca4        2           24 System.Text.DecoderExceptionFallback
79103c58        2           24 System.Text.EncoderExceptionFallback
502254bc        2           40 System.ServiceModel.Diagnostics.ExceptionUtility
790fe17c        1           72 System.ExecutionEngineException
790fe0e0        1           72 System.StackOverflowException
790fe044        1           72 System.OutOfMemoryException
6640ab48        1          104 System.Web.UI.ViewStateException
20f199b4        1          112 Microsoft.ReportingServices.Diagnostics.Utilities.InternalCatalogException
790fe284        2          144 System.Threading.ThreadAbortException
663dc0d4        2          168 System.Web.HttpException
79118794       11          352 System.UnhandledExceptionEventHandler

We have a ViewStateException so for sure it worth digging into it using the MetodTable:

0:033> !dumpheap -mt 6640ab48        
------------------------------
Heap 0
 Address       MT     Size
total 0 objects
------------------------------
Heap 1
 Address       MT     Size
042c74dc 6640ab48      104     
total 1 objects
------------------------------
Heap 2
 Address       MT     Size
total 0 objects
------------------------------
Heap 3
 Address       MT     Size
total 0 objects
------------------------------
Heap 4
 Address       MT     Size
total 0 objects
------------------------------
Heap 5
 Address       MT     Size
total 0 objects
------------------------------
Heap 6
 Address       MT     Size
total 0 objects
------------------------------
Heap 7
 Address       MT     Size
total 0 objects
------------------------------
total 1 objects
Statistics:
      MT    Count    TotalSize Class Name
6640ab48        1          104 System.Web.UI.ViewStateException
Total 1 objects


0:033> !do 042c74dc 
Name: System.Web.UI.ViewStateException
MethodTable: 6640ab48
EEClass: 6640aad0
Size: 104(0x68) bytes
 (C:\WINDOWS\assembly\GAC_32\System.Web\2.0.0.0__b03f5f7f11d50a3a\System.Web.dll)
Fields:
      MT    Field   Offset                 Type VT     Attr    Value Name
790fd8c4  40000b5        4        System.String  0 instance 042cbd04 _className
7910ebc8  40000b6        8 ...ection.MethodBase  0 instance 00000000 _exceptionMethod
790fd8c4  40000b7        c        System.String  0 instance 00000000 _exceptionMethodString
790fd8c4  40000b8       10        System.String  0 instance 00000000 _message
79116114  40000b9       14 ...tions.IDictionary  0 instance 00000000 _data
790fdf04  40000ba       18     System.Exception  0 instance 042c73c8 _innerException
790fd8c4  40000bb       1c        System.String  0 instance 00000000 _helpURL
790fd0f0  40000bc       20        System.Object  0 instance 00000000 _stackTrace
790fd8c4  40000bd       24        System.String  0 instance 00000000 _stackTraceString
790fd8c4  40000be       28        System.String  0 instance 00000000 _remoteStackTraceString
79102290  40000bf       34         System.Int32  1 instance        0 _remoteStackIndex
790fd0f0  40000c0       2c        System.Object  0 instance 00000000 _dynamicMethods
79102290  40000c1       38         System.Int32  1 instance -2146233088 _HResult
790fd8c4  40000c2       30        System.String  0 instance 00000000 _source
791016bc  40000c3       3c        System.IntPtr  1 instance        0 _xptrs
79102290  40000c4       40         System.Int32  1 instance -532459699 _xcode
7910be50  4002542       60       System.Boolean  1 instance        1 _isConnected
790fd8c4  4002543       44        System.String  0 instance 042ca6b4 _remoteAddr
790fd8c4  4002544       48        System.String  0 instance 042ca8cc _remotePort
790fd8c4  4002545       4c        System.String  0 instance 042bd764 _userAgent
790fd8c4  4002546       50        System.String  0 instance 042c16ec _persistedState
790fd8c4  4002547       54        System.String  0 instance 042bd62c _referer
790fd8c4  4002548       58        System.String  0 instance 042bbe48 _path
790fd8c4  4002549       5c        System.String  0 instance 042cb178 _message
7910be50  400254a       61       System.Boolean  1 instance        1 _macValidationError

The _message field is as follows:

0:033> !do 042cb178 
Name: System.String
MethodTable: 790fd8c4
EEClass: 790fd824
Size: 2104(0x838) bytes
 (C:\WINDOWS\assembly\GAC_32\mscorlib\2.0.0.0__b77a5c561934e089\mscorlib.dll)
String: Invalid viewstate. 
    Client IP: <ip_address>
    Port: 1449
    User-Agent: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; .NET CLR 2.0.50727; .NET CLR 1.1.4322; .NET CLR 3.0.04506.30)
    ViewState: /wEPDwUJNDY5NzIzNzI1D2QWAgIDD2QWAgIBDxQrAAQPFg4eDkFzeW5jUmVuZGVyaW5nZx4UU2hvd1Byb21wdEFyZWFCdXR0b25nHgtab29tUGVyY2VudAJkHghab29t
TW9kZQsphgFNaWNyb3NvZnQuUmVwb3J0aW5nLldlYkZvcm1zLlpvb21Nb2RlLCBSZXBvcnRpbmdTZXJ2aWNlc1dlYlNlcnZlciwgVmVyc2lvbj05LjAuMjQyLjAsIEN1bHR1cmU9bmV1
dHJhbCwgUHVibGljS2V5VG9rZW49ODk4NDVkY2Q4MDgwY2M5MQIeFERvY3VtZW50TWFwQ29sbGFwc2VkaB4OUmVuZGVyaW5nU3RhdGULKZIBTWljcm9zb2Z0LlJlcG9ydGluZy5XZWJGb
3Jtcy5SZXBvcnRSZW5kZXJpbmdTdGF0ZSwgUmVwb3J0aW5nU2VydmljZXNXZWJTZXJ2ZXIsIFZlcnNpb249OS4wLjI0Mi4wLCBDdWx0dXJlPW5ldXRyYWwsIFB1YmxpY0tleVRva2Vu
PTg5ODQ1ZGNkODA4MGNjOTECHhBWMVN0eWxlU2hlZXROYW1lZGQoKVhTeXN0ZW0uR3VpZCwgbXNjb3JsaWIsIFZlcnNpb249Mi4wLjAuMCwgQ3VsdHVyZT1uZXV0cmFsLCBQdWJsaWNL
ZXlUb2tlbj1iNzdhNWM1NjE5MzRlMDg5JGI2MzVjZjFkLTg4MGYtNDEyMC05Y2RjLTk2ZTE0ZGViYzRiMQIBFCsAARQrAAICARQrAAIFGGx3Y2d4MGIzZGI1MzB4NTVsY2F2eXA1NQIB
FgpmDw8WAh4LSG...
Fields:
      MT    Field   Offset                 Type VT     Attr    Value Name
79102290  4000096        4         System.Int32  1 instance     1044 m_arrayLength
79102290  4000097        8         System.Int32  1 instance     1043 m_stringLength
790ff328  4000098        c          System.Char  1 instance       49 m_firstChar
790fd8c4  4000099       10        System.String  0   shared   static Empty
    >> Domain:Value  000d49f0:790d884c 00101438:790d884c 1f7865b8:790d884c <<
7912dd40  400009a       14        System.Char[]  0   shared   static WhitespaceChars
    >> Domain:Value  000d49f0:0a2203f4 00101438:0a2244e8 1f7865b8:0226db3c <<

The ViewState in this error message is truncated, but we can get the entire string from _persistedState:

0:033> !do 042c16ec 
Name: System.String
MethodTable: 790fd8c4
EEClass: 790fd824
Size: 10162(0x27b2) bytes
 (C:\WINDOWS\assembly\GAC_32\mscorlib\2.0.0.0__b77a5c561934e089\mscorlib.dll)
String: /wEPDwUJNDY5NzIzNzI1D2QWAgIDD2QWAgIBDxQrAAQPFg4eDkFzeW5jUmVuZGVyaW5nZx4UU2hvd1Byb21wdEFyZWFCdXR0b25nHgtab29tUGVyY2VudAJkHghab29tTW9kZ
QsphgFNaWNyb3NvZnQuUmVwb3J0aW5nLldlYkZvcm1zLlpvb21Nb2RlLCBSZXBvcnRpbmdTZXJ2aWNlc1dlYlNlcnZlciwgVmVyc2l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0bDAxJGN0bDA1JGN0bDAxJGN0bDAwBTFSZXBvcnRWaWV3ZXJDb250cm9sJGN0bDAxJGN0b
DAxJGN0bDA2JGN0bDAwJGN0bDAwBTFSZXBvcnRWaWV3ZXJDb250cm9sJGN0bDAxJGN0bDAxJGN0bDA2JGN0bDAxJGN0bDAwBTFSZXBvcnRWaWV3ZXJDb250cm9sJGN0bDAxJGN0bDAyJ
GN0bDAwJGN0bDAwJGN0bDAwBTFSZXBvcnRWaWV3ZXJDb250cm9sJGN0bDAxJGN0bDAyJGN0bDAwJGN0bDAxJGN0bDAwBStSZXBvcnRWaWV3ZXJDb250cm9sJGN0bDAxJGN0bDA2JGN0b
DAwJGN0bDAwBTFSZXBvcnRWaWV3ZXJDb250cm9sJGN0bDAxJGN0bDA3JGN0bDAwJGN0bDAwJGN0bDAwBTFSZXBvcnRWaWV3ZXJDb250cm9sJGN0bDAxJGN0bDA3JGN0bDAwJGN0bDAxJ
GN0bDAwBTFSZXBvcnRWaWV3ZXJDb250cm9sJGN0bDAxJGN0bDA4JGN0bDAwJGN0bDAwJGN0bDAwBTFSZXBvcnRWaWV3ZXJDb250cm9sJGN0bDAxJGN0bDA4JGN0bDAwJGN0bDAxJGN0b
DAwX2RaxoBglgh8qbVp7F4IKaK6C+A=
Fields:
      MT    Field   Offset                 Type VT     Attr    Value Name
79102290  4000096        4         System.Int32  1 instance     5073 m_arrayLength
79102290  4000097        8         System.Int32  1 instance     5072 m_stringLength
790ff328  4000098        c          System.Char  1 instance       2f m_firstChar
790fd8c4  4000099       10        System.String  0   shared   static Empty
    >> Domain:Value  000d49f0:790d884c 00101438:790d884c 1f7865b8:790d884c <<
7912dd40  400009a       14        System.Char[]  0   shared   static WhitespaceChars
    >> Domain:Value  000d49f0:0a2203f4 00101438:0a2244e8 1f7865b8:0226db3c <<

There is also an _innerException we can look at:

0:033> !do 042c73c8 
Name: System.Web.HttpException
MethodTable: 663dc0d4
EEClass: 663dc05c
Size: 84(0x54) bytes
 (C:\WINDOWS\assembly\GAC_32\System.Web\2.0.0.0__b03f5f7f11d50a3a\System.Web.dll)
Fields:
      MT    Field   Offset                 Type VT     Attr    Value Name
790fd8c4  40000b5        4        System.String  0 instance 042cbd58 _className
7910ebc8  40000b6        8 ...ection.MethodBase  0 instance 00000000 _exceptionMethod
790fd8c4  40000b7        c        System.String  0 instance 00000000 _exceptionMethodString
790fd8c4  40000b8       10        System.String  0 instance 042c745c _message
79116114  40000b9       14 ...tions.IDictionary  0 instance 00000000 _data
790fdf04  40000ba       18     System.Exception  0 instance 00000000 _innerException
790fd8c4  40000bb       1c        System.String  0 instance 00000000 _helpURL
790fd0f0  40000bc       20        System.Object  0 instance 042c74a0 _stackTrace
790fd8c4  40000bd       24        System.String  0 instance 00000000 _stackTraceString
790fd8c4  40000be       28        System.String  0 instance 00000000 _remoteStackTraceString
79102290  40000bf       34         System.Int32  1 instance        0 _remoteStackIndex
790fd0f0  40000c0       2c        System.Object  0 instance 00000000 _dynamicMethods
79102290  40000c1       38         System.Int32  1 instance -2147467259 _HResult
790fd8c4  40000c2       30        System.String  0 instance 00000000 _source
791016bc  40000c3       3c        System.IntPtr  1 instance        0 _xptrs
79102290  40000c4       40         System.Int32  1 instance -532459699 _xcode
79102290  4001029       48         System.Int32  1 instance        0 _httpCode
663e49c4  400102a       44 ...eb.ErrorFormatter  0 instance 00000000 _errorFormatter
79102290  400102b       4c         System.Int32  1 instance        0 _webEventCode


0:033> !do 042c745c 
Name: System.String
MethodTable: 790fd8c4
EEClass: 790fd824
Size: 66(0x42) bytes
 (C:\WINDOWS\assembly\GAC_32\mscorlib\2.0.0.0__b77a5c561934e089\mscorlib.dll)
String: Unable to validate data.
Fields:
      MT    Field   Offset                 Type VT     Attr    Value Name
79102290  4000096        4         System.Int32  1 instance       25 m_arrayLength
79102290  4000097        8         System.Int32  1 instance       24 m_stringLength
790ff328  4000098        c          System.Char  1 instance       55 m_firstChar
790fd8c4  4000099       10        System.String  0   shared   static Empty
    >> Domain:Value  000d49f0:790d884c 00101438:790d884c 1f7865b8:790d884c <<
7912dd40  400009a       14        System.Char[]  0   shared   static WhitespaceChars
    >> Domain:Value  000d49f0:0a2203f4 00101438:0a2244e8 1f7865b8:0226db3c <<

So we know in this case we are unable to validate the ViewState; we can also paste the ViewState string from _persistedState in ViewState Decoder

view state decoder
view state decoder

The “Character Count” value is just a bit more than 5.000, not much for the complex pages we were looking at… Depending on their configuration, firewall and/or proxies software can truncate fields and values exceeding a certain amount of bytes, and this is another known cause for InvalidViewstateExceptions. Who can we quickly check?

A network trace can help, but as an empiric test we can use LogParser on IIS logs. These are the maximum, average and minimum amount of bytes send from the server as a result of a GET to an aspx page:

logparser 
    "select max(sc-bytes), avg(sc-bytes), min(sc-bytes) from *.log 
    where to_lowercase(extract_extension(cs-uri-stem))='aspx' and to_lowercase(cs-method)='get'" -i:iisw3c

MAX(ALL sc-bytes) AVG(ALL sc-bytes) MIN(ALL sc-bytes)
----------------- ----------------- -----------------
72756             44009             448

And these are the same values, calculated on the bytes posted from the clients:

logparser 
    "select max(cs-bytes), avg(cs-bytes), min(cs-bytes) from *.log 
    where to_lowercase(extract_extension(cs-uri-stem))='aspx' and to_lowercase(cs-method)='post'" -i:iisw3c

MAX(ALL cs-bytes) AVG(ALL cs-bytes) MIN(ALL cs-bytes)
----------------- ----------------- -----------------
11662             4620              819

Uhm… the server is sending an average of about 44 Kb and the clients are posting an average of less than 5 Kb… also the maximum values are quite different. Not a crushing proof but still something to keep in mind…

A quick check on customer’s network revealed they actually had configured (I think for security reasons) a maximum allowed size for HTML incoming fields and due to some policies and constraints they had, we could not increase that value. Ok, now what?

Enter MaxPageStateFieldLength

Luckily, we can configure the MaxPageStateFieldLength property exactly for this purpose:

When the MaxPageStateFieldLength property is set to a positive number, the view state sent to the client browser is broken into multiple hidden fields, and each field’s value is less than the size specified in the MaxPageStateFieldLength property.

Setting the MaxPageStateFieldLength property to a negative number (the default) indicates that the view-state field should not be separated into chunks. Setting the MaxPageStateFieldLength to a small number may result in poor performance.

Set the value of the MaxPageStateFieldLength property in the pages element of the Web.config file.

We set the values to something less than 5 Kb and the problem magically disappeared ?

Carlo

Quote of the day:

Have a heart that never hardens, and a temper that never tires, and a touch that never hurts — Charles Dickens

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.