ASP.NET: The View State is invalid for this page and might be corrupted

I fixed a tricky bug yesterday on one of our sites that runs ASP.NET. Like all good webmasters, anytime a 500 error occurs on the site, an email is sent to me that contains all the juicy details: the URL the user was visiting, the date/time, what browser they were using, which server in the cluster it occured on, the stack trace, any form variables, the querystring, session variables, and cookies. This particular error would occur on any page with a form and the stack trace would look like this:

Exception of type System.Web.HttpUnhandledException was thrown.
source = System.Web
target site = Boolean HandleError(System.Exception)
stack trace = at System.Web.UI.Page.HandleError(Exception e) at
System.Web.UI.Page.ProcessRequestMain() at
System.Web.UI.Page.ProcessRequest() at System.Web.UI.Page.ProcessRequest(HttpContext context) at System.Web.CallHandlerExecutionStep.System.Web.HttpApplication+IExecutio
nStep.Execute() at System.Web.HttpApplication.ExecuteStep(IExecutionStep
step, Boolean& completedSynchronously)

which isn’t very helpful at all. So my first problem was that I wasn’t getting enough information; I couldn’t tell what the problem really was. The HttpModule that I wrote that listens for Application_Error events was retrieving the exception using this code:

HttpApplication application = (HttpApplication)source;
Exception error = application.Server.GetLastError();

which technically does retrieve the error that caused the event, but I was missing one key step: the last line should have read:

Exception error = application.Server.GetLastError().InnerException;

and once I fixed that, I magically started receiving a much richer error description:

System.Web.HttpException: The viewstate is invalid for this page and might be corrupted. at System.Web.UI.Page.LoadPageStateFromPersistenceMedium() at System.Web.UI.Page.LoadPageViewState() at System.Web.UI.Page.ProcessRequestMain()

That description leads to a couple different knowledge base articles on support.microsoft.com; Q323744 and Q312906 were the ones that fixed the problem I was experiencing. Turns out that when you a) run an ASP.NET application in a cluster (in my case it’s an ASP.NET application load balanced behind with an F5), b) don’t use sticky sessions, and c) utilize view state, you must setup your web servers to use the exact same machine key, which is a setting in machine.config. Microsoft supplies a C# script that will generate the appropriate machineKey element for you machine.config in Q312906.

4 thoughts on “ASP.NET: The View State is invalid for this page and might be corrupted”

  1. Aaron,

    We have the same problem. We have tried all the solutions listed under MS Support articles (and still working with MS to figure out the issue). We have F5 (big-ip) load balancer with Sticky sessions turned on. The issue is, Authentication is lost & user is kicked out to login page intermittently. So far, we have tried:
    1. Time-out set to higher rate (and turn Sliding expiration on).
    2. Path in Web.config for the Auth cookie is “/”
    3. Session expires after 29 hours.
    4. Captures Viewstate Log, Forms Auth log – found the auth ticket is lost intermittently. So now we are trying to get the NetMon trace to see if the network is causing some packets to be lost (per MS suggestion!)

    I guess if you have any sugesstions please email me -2005jay@gmail.com

    Thanks a ton!

  2. Hi all! Mi name is Michael. I’m from Costa Rica and I’m a .net programmer. But I need some help.

    I’ve got an error working with F5 Big Ip virtual farm:

    This is my scenario.

    1-I’ve developed a .net 2.0 remoting application.

    2-I’m using four Windows Services to host my remoting objects.

    3-I’m using TCP Channel and SAO activation.

    4-This is the Windows Services .app config

    5- This is the Client Activation:

    CType(Activator.GetObject(GetType(Interfaces.Operaciones.ICierresDiarios), Aplicacion.TraerURLOperaciones & “/CierresDiarios”), Interfaces.Operaciones.ICierresDiarios)

    6-I have a Middleware farm (F5 BIGIP) address (192.168.162.162). This farm has two nodes (192.168.162.50, 192.168.162.51).This nodes has the same hardware and software specification. Both of them have installed my Windows Services.

    7-When I try to connect thought out farm (192.168.162.162) I get this error “An active connection was forcibly closed by remote host” or even:

    System.Net.Sockets.SocketException: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond Server stack trace: at System.Net.Sockets.Socket.DoConnect(EndPoint endPointSnapshot, SocketAddress socketAddress) at System.Net.Sockets.Socket.Connect(EndPoint remoteEP) at System.Runtime.Remoting.Channels.RemoteConnection.CreateNewSocket(EndPoint ipEndPoint) at System.Runtime.Remoting.Channels.RemoteConnection.CreateNewSocket() at System.Runtime.Remoting.Channels.SocketCache.GetSocket(String machinePortAndSid, Boolean openNew) at System.Runtime.Remoting.Channels.Tcp.TcpClientTransportSink.SendRequestWithRetry(IMessage msg, ITransportHeaders requestHeaders, Stream requestStream) at System.Runtime.Remoting.Channels.Tcp.TcpClientTransportSink.ProcessMessage(IMessage msg, ITransportHeaders requestHeaders, Stream requestStream, ITransportHeaders& responseHeaders, Stream& responseStream) at System.Runtime.Remoting.Channels.BinaryClientFormatterSink.SyncProcessMessage(IMessage msg) Exception rethrown at [0]: at System.Runtime.Remoting.Proxies.RealProxy.HandleReturnMessage(IMessage reqMsg, IMessage retMsg) at System.Runtime.Remoting.Proxies.RealProxy.PrivateInvoke(MessageData& msgData, Int32 type) at

    8 I try to connect to a single node 192.168.162.50 OR 192.168.162.51 individually and I don’t get any error.

    I’ve noticed that the network manager created just a Http Profile on F5, is this right?

    Thanks for your help!.

  3. Hi all,

    My site having smile problem as below. Could somebody help me to resolve this problem?

    Server Error in ‘/GenericWebbookCC’ 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\genericwebbookcc\88714d05\b5a2d222\App_Web_default.aspx.cdcab7d2.p16vjrnz.0.cs Line: 0

    Stack Trace:

    [ViewStateException: Invalid viewstate.
    Client IP: 127.0.0.1
    Port:
    User-Agent: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.2; .NET CLR 1.1.4322; .NET CLR 2.0.50727; .NET CLR 3.0.04506.648; .NET CLR 3.5.21022)
    ViewState: /wEWdQKV0KTHDwLzsONuAoGT+bULAqmVsPoIAsDvr/0DAt/vr/0DAtCAhZMPAoTm9BgCg+aYGwKD5pQbAoPmkBsCguaUGwKIifL2DALf8NKwDALA8NKwDALPn/heAsW8mNYBAt6z5+sLAsGz5+sLAs7czYUHAu/shNkCAtDfxO4EAvaD4ugCAqns3JgOAqns2JgOAqnsgJgOAqns4JkOAqjs/JgOAqvsxJgOAqvszJgOAqvsuJgOAqrs/JgOAqrs9JgOAqzs2JgOAq/sxJgOAp7spJgOApHsxJgOApHs+JgOApHs2JgOApHs0JgOApPsvJgOApPs5JkOApLsxJgOApXsxJgOApXs+JgOApXs9JgOApXspJgOApXs0JgOApXszJgOApXsvJgOApXsuJgOApTs/JgOApTs+JgOApTs9JgOApTsqJgOApTsoJgOApTs1JgOApTssJgOApTs5JkOApfsqJgOApfs3JgOApfs0JgOApfsgJgOAobsxJgOApjspJgOApvs/JgOApvs+JgOAprs0JgOAprs6JkOAp3suJgOApzsxJgOApzsuJgOAp/sxJgOAp/spJgOAp/ssJgOAp/s5JkOAvy/+ZAHAunar+MKApz+8rsJApnJ7xEC8LyVdALg44jgAgL/44jgAgLvjKKODgLjluj/AwLgluj/AwLlu/vwAgLtqKfMAwLqnq4tAt7L6ogFAuur+sUGAtKor8AFAoz885MDAvup2qEJAvSjz7ACAvrSnI4HAvrDt60LAsCbxtIIArW25scDArK2isQDArK2hsQDArK2gsQDArO2hsQDArnZ4KkPAqq…]

    [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) +170
    System.Web.UI.ViewStateException.ThrowViewStateError(Exception inner, String persistedState) +37
    System.Web.UI.ClientScriptManager.EnsureEventValidationFieldLoaded() +371
    System.Web.UI.ClientScriptManager.ValidateEvent(String uniqueId, String argument) +202
    System.Web.UI.Control.ValidateEvent(String uniqueID, String eventArgument) +83
    System.Web.UI.WebControls.RadioButtonList.LoadPostData(String postDataKey, NameValueCollection postCollection) +242
    System.Web.UI.WebControls.RadioButtonList.System.Web.UI.IPostBackDataHandler.LoadPostData(String postDataKey, NameValueCollection postCollection) +36
    System.Web.UI.Page.ProcessPostData(NameValueCollection postData, Boolean fBeforeLoad) +478
    System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +5899
    System.Web.UI.Page.ProcessRequest(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +281
    System.Web.UI.Page.ProcessRequest() +112
    System.Web.UI.Page.ProcessRequestWithNoAssert(HttpContext context) +37
    System.Web.UI.Page.ProcessRequest(HttpContext context) +135
    ASP.default_aspx.ProcessRequest(HttpContext context) in c:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\Temporary ASP.NET Files\genericwebbookcc\88714d05\b5a2d222\App_Web_default.aspx.cdcab7d2.p16vjrnz.0.cs:0
    System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +713
    System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +117

    ——————————————————————————–
    Version Information: Microsoft .NET Framework Version:2.0.50727.1433; ASP.NET Version:2.0.50727.1433

  4. Hey thanks a million, was having a nightmare with this as all I was getting was UnhandledHttpException but your fix did it for me, one thing I would suggest tho is that sometimes the InnerException is null so test for that first otherwise your error-handling code will itself cause an error, also the innerException can have an innerException so you might want to try looping through until no more innerexceptions are found

Leave a Reply

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