Saturday, February 12, 2005

"viewstate is invalid for this page and might be corrupted"

I didn't know much about .Net viewstates until yesterday when I needed to configure a .Net application living on multiple servers behind a load balancer. Suffice it to say that you need to generate an encrypted key and configure all servers to use the same key, rather than using machine.config's "AutoGenerate" setting:
machineKey validationKey="AutoGenerate,IsolateApps" decryptionKey="AutoGenerate,IsolateApps" validation="SHA1"

Here is an interesting article that tells you how to do this:

Viewstate isn't really the same as session state. Viewstate is a hidden field that stores page state between calls. The way that viewstate works is:
1. When returns an http response to a user, it compiles all the data field data into a big encrypted string.
2. The string is passed as a hidden field. When the user responds to the page, the viewstate is passed back to the server.
4. ASP.NET deserializes and decrypts the string to refill the form fields.
5. If the deserialization breaks, then you get the error "viewstate is invalid for this page and might be corrupted."

Viewstate can break if:
1. The servers aren't all using the same encryption/decryption keys.
2. The viewstate field is being truncated.
3. Other scenarios. Ahem.

No comments:

Feel free to drop me a line or ask me a question.