Validation of viewstate MAC failed

This forum is only for questions or discussions about working with the mojoPortal source code in Visual Studio, obtaining the source code from the repository, developing custom features, etc. If your question is not along these lines this is not the right forum. Please try to post your question in the appropriate forum.

Please do not post questions about design, CSS, or skinning here. Use the Help With Skins Forum for those questions.

This forum is for discussing mojoPortal development

This forum is only for questions or discussions about working with the mojoPortal source code in Visual Studio, obtaining the source code from the repository, developing custom features, etc. If your question is not along these lines this is not the right forum. Please try to post your question in the appropriate forum.

You can monitor commits to the repository from this page. We also recommend developers to subscribe to email notifications in the developer forum as occasionally important things are announced.

Before posting questions here you might want to review the developer documentation.

Do not post questions about design, CSS, or skinning here. Use the Help With Skins Forum for those questions.
This thread is closed to new posts. You must sign in to post in the forums.
1/26/2012 1:46:41 PM
Gravatar
Total Posts 1203
Proud member of the mojoPortal team

Help support mojoPortal!
Add-on modules

Validation of viewstate MAC failed

Hi Joe, I've had some reports of errors when clicking a button in our custom features after upgrade, and looking at it with error suppression off shows that it's throwing the following exception:

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.

Before I spend a lot of time researching, I was hoping you might have some ideas off the top of your head about what might have changed in the codeline over the past few months to start causing this. It's going to be tough to debug since the error is thrown before the button click event is firing. No need for a lot of research, but if you have any pointers I'd appreciate it.

Thanks,
Jamie

1/26/2012 2:16:43 PM
Gravatar
Total Posts 18439

Re: Validation of viewstate MAC failed

Hi Jamie,

There are lots of things that can cause that, such as clicking a button before the page is fully loaded, and other things. I would google for "causes of Validation of viewstate MAC failed" and review the things you find other than the most common one about machine keys and web farms which is not the cause in this case.

I don't believe it is because of any recent change in mojoPortal. You may need to have Page.EnableViewState = true; to make sure viewstate isn't disabled in places where you need it.

Hope that helps,

Joe

1/30/2012 4:59:56 PM
Gravatar
Total Posts 1203
Proud member of the mojoPortal team

Help support mojoPortal!
Add-on modules

Re: Validation of viewstate MAC failed

Hi Joe, I'm still struggling with this one. I spent about four hours on Thursday night trying to get to the bottom of this. I determined it was only happening in cases where viewstate is encrypted (ASP grid viewstate is automatically encrypted by default). To get it working I finally punted by setting

<pages ... viewStateEncryptionMode="Never" ...>

We only have one custom feature that's using viewstate and explicitly encrypting it, so I turned that off temporarily until I could spend more time on the problem (encryption is an extra safeguard for that case but not mission critical).

So today I'm back at it, and I'm thinking I may be seeing the problem shown in this blog post. When I view source for the page with our custom feature, I see that the form element has an action equal to the page URL:

<form method="post" action="/parcel-lookup.aspx" id="aspnetForm">

However, when I have viewStateEncryptionMode set to "Auto" and get the error message, I see the following entries in the detailed error message, which sounds like the problem they describe:

Referer: http://wwwlocal.escondido.org/parcel-lookup.aspx
Path: /Default.aspx

Do you know why this might be happening and how to sync those two up? Also just FYI, there is another page with a delivered poll feature on it that's exhibiting the same issue, so that really gives me some hope that this isn't just something boneheaded I've done within my custom code. Smile Finally, in case it helps, sometimes the error won't fire when I'm logged in, but happens when I'm logged out. Argh!

Thanks,
Jamie

1/30/2012 9:08:54 PM
Gravatar
Total Posts 355

Re: Validation of viewstate MAC failed

Just FYI: I get the same "Validation of viewstate MAC failed" error, about once a month or two; I end up just recycling the IIS thread, and it's back to business as usual. I'd presumed that it was due to some aspect of my hosting configuration, as there doesn't seem to be any specific triggers, nor does it happen to my other client sites. I'll be watching this thread to see what y'all come up with.

1/31/2012 6:23:58 AM
Gravatar
Total Posts 18439

Re: Validation of viewstate MAC failed

Hi Jamie,

Is there an updatepanel involved in your custom feature?

If so try adding this in PageLoad or something that is called from PageLoad on every request ie both get and poostback requests.

try
{
// this keeps the action from changing during ajax postback
SiteUtils.SetFormAction(Page, Request.RawUrl);
}
catch (MissingMethodException)
{
//this method was introduced in .NET 3.5 SP1
}

You may not need the try catch, I just have that because we support .NET 3.5 sp1 and it throws an error if sp1 is not installed.

Another thing you can try is adding this to user.config:

<add key="UseAjaxFormActionUpdateScript" value="false" />

maybe doing one or both of those will solve it.

The other things to consider is do you really need the viewstate encrypted on the grid? Is there any sensitive data bound to that or any other grids in your site that actually needs to be encrypted? If not then it seems a reasonable solution to turn that off. If there is sensitive stuff shown on the page I would use SSL.

Hope that helps,

Joe

5/30/2012 1:45:25 PM
Gravatar
Total Posts 1203
Proud member of the mojoPortal team

Help support mojoPortal!
Add-on modules

Re: Validation of viewstate MAC failed

Okay, I believe I have finally figured this one out, through extensive trial and error.

We have SSL installed, so as recommended in web.config we are using the <forms> variant with requireSSL="true", and also the secondary <httpCookies requireSSL="true"/> setting.

With this configuration in place, and <pages ... viewStateEncryptionMode="Auto"...> set, a couple of my custom features were throwing the viewstate error, but only on pages that were viewed in non-SSL mode. No error was thrown when in SSL, and this was the key.

I discovered today that if I comment out the <httpCookies requireSSL="true"/> setting in web.config, I don't get the viewstate errors, whether the page in is SSL mode or not!

The best news is that according to this page on MSDN, it looks like <httpCookies requireSSL="true"/> is a redundant setting anyway, since the <forms requireSSL="true"> will override it. So all cookies should be protected, not just the authentication cookies.

Joe, you may want to remove that httpCookies reference completely from web.config since it's not needed and seems to cause an issue at least in my case.

Jamie

 

5/30/2012 2:09:03 PM
Gravatar
Total Posts 18439

Re: Validation of viewstate MAC failed

Hi Jamie,

My interpretation of the documentation you linked is that it means if requireSSL is false on the <httpCookies element but true on the <forms element then it will be overridden to true for forms auth cookie, not that it will affect all other cookies when you set it to true on the <forms element. It just means that features that have their own setting for secure cookies will trump this general setting on the httpCookies element.

However we do have our own check in code to force the role cookie to be secure, so we don't really need that setting to ensure the security of the role cookie, so if it is causing other problems I guess we can safely remove that httpCookies setting.

if ((SiteUtils.SslIsAvailable()) && WebConfigSettings.RequireSslForRoleCookie)
{
         roleCookie.Secure = true;
}

Best,

Joe

5/30/2012 2:20:41 PM
Gravatar
Total Posts 1203
Proud member of the mojoPortal team

Help support mojoPortal!
Add-on modules

Re: Validation of viewstate MAC failed

Thanks Joe!

You must sign in to post in the forums. This thread is closed to new posts.