in

SharePoint Blogs

The Best Place for SharePoint-related Blogs

Nick's SharePoint Blog

Edit Sharepoint security in C# -- "The security validation for this page is invalid"

I recently had my fair share of trouble when trying to edit the security of a web site through code.

Every time I executed the code, some error came up. Most of the time, it was either "Access Denied" or "The security validation for this page is invalid".
Pretty annoying stuff I thought, and searching the web didn't really help all that much ...

I ran the code using RunWithElevatedPrivileges, but that didn't help much. Neither did the SPWeb.AllowUnsafeUpdates property.

After searching for a REALLY long time, I found the answer in a blog's comment:
(http://spiderwool.blogspot.com/2006/07/security-validation-for-this-page-is.html)

SPSite.WebApplication.FormDigestSettings.Enabled = false

Finally I had found the solution.
However, after redeploying the code on a new web application, I suddenly got an Access Denied error when trying to set this property.
After some searching I found out this was due to the fact I set the application pool to run as Network Service in stead of an administrative account.
This did fix my problem, however I did not really found out the actual source of the issue ...

So, taking all this into account, here is an example of how to set a web's security through code:

public void EditSecurity()

{

    SPSecurity.RunWithElevatedPrivileges(delegate()

    {

        using (SPSite site = new SPSite(url))

        {

            using (SPWeb web = site.OpenWeb())

            {

                SPWebApplication webApp = web.Site.WebApplication;

                webApp.FormDigestSettings.Enabled = false;

                web.AllowUnsafeUpdates = true;

 

                SPGroup group = web.SiteGroups["groupname"];

                SPRoleAssignment roleAssignment = new SPRoleAssignment((SPPrincipal)group);

 

                SPRoleDefinition roleDefinition;

                roleDefinition = web.RoleDefinitions.GetByType(SPRoleType.Contributor); // Gets a predefined role definition

                roleDefinition = web.RoleDefinitions["customRole"]; // Gets a custom defined role definition

 

                roleAssignment.RoleDefinitionBindings.Add(roleDefinition);

 

                web.RoleAssignments.Add(roleAssignment);

 

                web.Update();

                web.AllowUnsafeUpdates = false;

                webApp.FormDigestSettings.Enabled = true;

            }

        }

    });

}

So, to summarize:

  • Run the code with or with Elevated Privileges.
  • Set the web application's FormDigestSettings to disabled for the time you run your code.
  • Set the AllowUnsafeUpdates of the SPWeb object to true for the time you run your code.
  • Update the web object after executing the code.

PS: If you would get an Access Denied error at the setting of the FormDigestSettings, and e.g. you cannot change the web application's application pool identity, or you just can't seem to fix it, you can run your code without setting the FormDigestSettings in it. Alternatively, you can disable the page validation in the Web Application's Generel Settings in the Central Administration. To do this, go to Central Administration --> Application Management --> Web application general settings --> Security Validation = Off

UPDATE: Also take a look at http://epham.wordpress.com/2007/01/22/how-to-fix-security-validation-errors-in-sharepoint-aspnet-page which in some cases also might provide a solution for the issue.

Published Nov 23 2007, 08:55 AM by nsevens
Filed under: , , ,

Comments

 

Dinheiro Internet - Blog de Dinheiro » Edit Sharepoint security in C# — “The security validation for this page is invalid” said:

Pingback from  Dinheiro Internet - Blog de Dinheiro » Edit Sharepoint security in C# — “The security validation for this page is invalid”

November 23, 2007 4:48 AM
 

jianhui said:

hi, I have a question that a form user use the code will show one error message. if it 's a windows user , the code is ok. Are you have some solvent,thanks.

December 27, 2007 3:10 PM
 

Johan said:

I also have this problem. When i set FormDigestSettings.Enabled, I get exceptions. However I don't have the possibility to solve this by using the Network service. Does anybody have an idea?

January 16, 2008 2:33 AM
 

Bouha said:

hi,

the work around didn't work for me in this way.

What i have done is adding  :

webApp.Update() after each change of the FormDigest property, so finally i decided to put Security Validation = Off in the central administration.

There must be a clean way to resolve that issue !! anyone get it ?  

May 1, 2008 8:03 AM
 

ThomasA said:

Thanks, this worked in a situation where I was working with the content deployment API. I was getting the security validation exception while executing SPImport.Run(). I really have no idea why this was happening or why this solution worked but it did.

May 27, 2008 10:18 AM

Leave a Comment

(required )  
(optional )
(required )  
Add

Need SharePoint Training? Attend a SharePoint Bootcamp!

Posts (c) their respective authors. Everything else (c) 2007 SharePoint Experts