Wednesday, February 16, 2011

Using Elevated Privileges and AllowSafeUpdates Method - A Potential Security Risks

Although not recommended, there may be times when you need your code to perform certain functions that the current user does not have the necessary permissions to perform. I have such situation in one of our project, suppose that you wanted to provide add, edit or delete capability to all users who use your application, regardless of their permissions on the list. So how this would be achieved?

By using the SPSecurity class, it provides a method RunWithElevatedPrivileges that allows you to run a subset of code in the context of an account with higher privileges than the current user. You need to wrap the RunWithElevatedPrivileges method around your code, as shown below:    
<code>
    protected void btnAddListItem_Click(object sender, EventArgs e)

    {
        using (SPSite oSite = SPContext.Current.Site)  {

        // Run with an account with higher privileges than the current user

         SPSecurity.RunWithElevatedPrivileges(delegate(){

//code               

 }

});
}
}

No comments:

Post a Comment