Search This Blog

Saturday, February 26, 2011

Solution to error "Security Validation for this page is invalid"

Few days back I came across this issue. It popped up whenever the page is posted back to update content to the site. On further digging into it I found this was somewhat related to the action performed within elevated code(i.e. code within SPSecurity.RunWithElevatedPrivileges).

SharePoint keep track of the requests using the token unique to the user and his request. Whenever the code is elevated the context is changed from the user to the system account.


To ensure smooth operation without the Security Validation error we should add SPUtility.ValidateFormDigest() line just before elevating the code. This MSDN article also talks about the same.

1 comment:

vapcguy said...

I'd recommend looking at http://ktskumar.wordpress.com/2009/03/09/the-security-validation-for-this-page-is-invalid-click-back-in-your-web-browser-refresh-the-page-and-try-your-operation-again/#comment-602.
Putting in web.ValidateFormDigest();
actually prevented the real fix from working for me, which was to use
web.AllowUnsafeUpdates = true;
and I set
web.Site.WebApplication.FormDigestSettings.Enabled = false;
and set it back to true at the end of my code. I might not have needed to do both the AllowUnsafeUpdates and doing the FormDigestSettings.Enabled = false, but when I saw it work I didn't want to go any further :)
-Tom