Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Asp.net Validation of viewstate MAC failed

I am receiving the following error at certain times on asp.net website.

 Sys.WebForms.PageRequestManagerServerErrorException:   Validation of viewstate MAC failed.   If this application is hosted by a Web Farm or cluster,  ensure that <machineKey> configuration specifies the   same validationKey and validation algorithm.   AutoGenerate cannot be used in a cluster. 

When page refresh goes,no problem.How can I solve this problem?

like image 989
Arbelac Avatar asked Apr 30 '11 09:04

Arbelac


People also ask

What is EnableViewStateMAC in web config?

The MAC ensures that the client hasn't tampered with these fields. When EnableViewStateMac is set to true, this code is validated by the server when the client submits the __VIEWSTATE hidden form field during post back. This setting has been enabled (true) by default for all versions of ASP.NET.

How do I fix invalid ViewState error?

The best fix to this problem is to avoid using binary serialization. Binary serialization uses many resources even when you do not run into this problem. Instead, limit what you put in view state to a combination of Arrays, Pairs, Triplets, and simple types (for example, strings, int, and other types).

How do I enable ViewState on MAC?

ASP.NET provides MAC validation for ViewState parameters by adding '<%@Page EnableViewStateMAC='True' %>' on the specific page or by adding '<pages enableViewStateMac='true'>' inside your web.


1 Answers

If you're using a web farm and running the same application on multiple computers, you need to define the machine key explicitly in the machine.config file:

<machineKey validationKey="JFDSGOIEURTJKTREKOIRUWTKLRJTKUROIUFLKSIOSUGOIFDS..." decryptionKey="KAJDFOIAUOILKER534095U43098435H43OI5098479854" validation="SHA1" /> 

Put it under the <system.web> tag.

The AutoGenerate for the machine code can not be used. To generate your own machineKey see this powershell script: https://support.microsoft.com/en-us/kb/2915218#bookmark-appendixa

like image 63
Maged Samaan Avatar answered Oct 24 '22 02:10

Maged Samaan