How to fix ViewStateException Invalid ViewState?

Usually you don't see Invalid ViewState exception unless you implement some kind of ASP.NET error logger in Global.asax.

Error usually states something like this:

Exception: System.Web.HttpException: The state information is invalid for this page and might be corrupted. ---> System.Web.UI.ViewStateException: Invalid viewstate.

Those errors are usually generated by some automatic submission form engines. Comments and contact forms are mostly effected by this exception. End user never sees those errors, so invalid viewstate errors don't have any value for your error logger. Let's bypass them:

if (ex is HttpException && ex.InnerException is ViewStateException)
{
    _app.Response.Redirect(_app.Request.Url.AbsoluteUri)                
    return
}

See my error logger article for further details.

Posted on Wednesday, June 23, 2010 by | Comments (6) | Add Comment

Comments

Gravatar

Re:How to fix ViewStateException Invalid ViewState?

thanks for the help.

Posted on 7/3/2010 10:43:15 AM by Surge #
Gravatar

Re:How to fix ViewStateException Invalid ViewState?

Hi,

What is the _app. ?

thanks.

Posted on 8/18/2010 6:47:07 AM by selcuk yazar #
Gravatar

Sorry,
I took this if statement from my logger code. _app is HttpContext.Current.ApplicationInstance.

Gravatar

Re:How to fix ViewStateException Invalid ViewState?

Server Error in '/MDCR' Application.
--------------------------------------------------------------------------------

The state information is invalid for this page and might be corrupted.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.Web.HttpException: The state information is invalid for this page and might be corrupted.

Source Error:


[No relevant source lines]


Source File: c:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\Temporary ASP.NET Files\mdcr\5c7639a0\5981344d\App_Web_oja12imh.0.cs Line: 0

Stack Trace:


[FormatException: Invalid character in a Base-64 string.]
System.Convert.FromBase64String(String s) +0
System.Web.UI.ObjectStateFormatter.Deserialize(String inputString) +77
System.Web.UI.ObjectStateFormatter.System.Web.UI.IStateFormatter.Deserialize(String serializedState) +4
System.Web.UI.Util.DeserializeWithAssert(IStateFormatter formatter, String serializedState) +37
System.Web.UI.HiddenFieldPageStatePersister.Load() +113

[ViewStateException: Invalid viewstate.
Client IP: 127.0.0.1
Port: 1486
User-Agent: Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0; .NET CLR 1.1.4322; .NET CLR 2.0.50727; .NET CLR 3.0.04506.30; .NET CLR 3.0.04506.648; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729; InfoPath.3; .NET4.0C; .NET4.0E)
ViewState: /wEPDwUKMTY5ODYyNzkxNQ9kFgICAw9kFgICAQ9kFgJmD2QWAmYPZBYEZg9kFgICAQ88KwAJAQAPFgYeDVNlbGVjdGVkSW5kZXgCDR4IRGF0YUtleXMWAB4LXyFJdGVtQ291bnQCDmQWHGYPZBYCAgEPDxYIHghUYWJJbmRleAEAAB4EVGV4dAUMTm90aWZpY2F0aW9uHgtDb21tYW5kTmFtZQUETW92ZR4PQ29tbWFuZEFyZ3VtZW50BQEwZGQCAQ9kFgICAQ8PFggfAwEAAB8EBQkxLkNvbXBhbnkfBQUETW92ZR8GBQExZGQCAg9kFgICAQ8PFggfAwEAAB8EBRMyLlR5cGUgb2YgT3duZXJzaGlwHwUFBE1vdmUfBgUBMmRkAgMPZBYCAgEPDxYIHwMBAAAfBAUUMy5FbXBsb3ltZW50IFNvdXJjZXMfBQUETW92ZR8GBQEzZGQCBA9kFgICAQ8PFggfAwEAAB8EBRU0LlN0YXRpc3RpY3M6IEN1cnJlbnQfBQUETW92ZR8GBQE0ZGQCBQ9kFgICAQ8PFggfAwEAAB8EBRQ1LkVtcGxveWVlczogQ3VycmVudB8FBQRNb3ZlHwYFATVkZAIGD2QWAgIBDw8WCB8DAQAAHwQFFzYuU3RhdGlzdGljczogTmV3IEhpcmVzHwUFBE1vdmUfBgUBNmRkAgcPZBYCAgEPDxYIHwMBAAAfBAUZNy5TdGF0aXN...]

[HttpException (0x80004005): The state information is invalid for this page and might be corrupted.]
System.Web.UI.ViewStateException.ThrowError(Exception inner, String persistedState, String errorPageMessage, Boolean macValidationError) +106
System.Web.UI.ViewStateException.ThrowViewStateError(Exception inner, String persistedState) +14
System.Web.UI.HiddenFieldPageStatePersister.Load() +217
System.Web.UI.Page.LoadPageStateFromPersistenceMedium() +105
System.Web.UI.Page.LoadAllState() +43
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +6785
System.Web.UI.Page.ProcessRequest(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +242
System.Web.UI.Page.ProcessRequest() +80
System.Web.UI.Page.ProcessRequestWithNoAssert(HttpContext context) +21
System.Web.UI.Page.ProcessRequest(HttpContext context) +49
ASP.certreviewform_aspx.ProcessRequest(HttpContext context) in c:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\Temporary ASP.NET Files\mdcr\5c7639a0\5981344d\App_Web_oja12imh.0.cs:0
System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +181
System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +7


I have this problem for the past two days and I am breaking my head. Please let me know how to by pass this error .
I am getting above error I don't how to solve it please help me.

Posted on 5/18/2011 5:28:02 PM by john oliveson #
Gravatar

Delete everything in c:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\Temporary ASP.NET Files\ and rebuild your solution.

Gravatar

Re:How to fix ViewStateException Invalid ViewState?

^have same problem, proposed solution didn't work for me:P any other ideas?:)

Posted on 11/2/2011 5:30:37 AM by Rufix #
Gravatar

Re: How to fix ViewStateException Invalid ViewState?

This error is usually caused by the asp worker process or the server recycling. By default, ASP.NET encrypts the viewstate using an Autogenerated Key when the process spins up. The problem comes when a client (browser) sends the request with a viewstate encrypted with the key generated by another worker process. Since the key is different, ASP.NET will not be able to decrypt the viewstate and it will throw the above error.

There are several ways to get around this problem:

1) Host your site on a server that never restarts or recycles!!! Obviously, this is impossible!

2) Disable ViewstateMac by putting this ?enableViewStateMac='false'? in your web.config. This approach is not 100% secure.

3) Configure ASP.NET to not use Auto-Generated Key but rather a predefined key. This is the preferred method.

To do this, follow these steps:

a) Either build your own Key Generator (http://support.microsoft.com/kb/313091/EN-US/) or use this tool (http://www.aspnetresources.com/tools/keycreator.aspx). I highly recommend you use the online tool. So assuming you will use the online tool:

b) In the online tool, simply click on ?Generate?.

c) Copy the content in the textbox into your site?s web.config file. The machineKey node should be within <system.web>

eg.

<configuration>
<appSettings/>
<connectionStrings/>
<system.web>
<machineKey
validationKey='2EEA416CEFC6D6BE856ED57B97FB9CA7DFA CE17C073125949A1D682C80A44BB2A
D887DDDC13DBFB0954F1000FEE5757E99693F222F8E28CAA2E 6DAB8C4F99E0C'
decryptionKey='877478B2F33A74226ABEF55FDCC1A76E43F 1BBEA6241A592'
validation='SHA1' />
<compilation debug='false'/>
<authentication mode='Windows'/>
<pages enableViewStateMac='true'/>
</system.web>
</configuration>

For more information on viewstate troubleshooting, see http://support.microsoft.com/default...b;EN-US;829743

Posted on 3/14/2012 8:17:40 AM by gma #
Gravatar

Re: How to fix ViewStateException Invalid ViewState?

Check this Article

http://blogs.msdn.com/b/tom/archive/2008/03/14/validation-of-viewstate-mac-failed-error.aspx

Posted on 8/2/2012 12:41:16 PM by Jay Nardev #

New Comment

Your Name:
Email (for internal use only):
Comment:
 
Code above:

Categories

Recent Tweets

  • Simon Ince's Blog: Hierarchies with HierarchyID in SQL 2008 http://t.co/xSDwiF6rRS.
  • Visual Studio 2010 WAS painfully slow - CodeProject http://t.co/Usba1x6CZy

Valid HTML5