Return to Snippet

Revision: 34794
at October 28, 2010 06:58 by scholli


Initial Code
SPWeb web = SPContext.Current.Web; 

//**************************************** 
// Validate the page request to avoid 
// any malicious posts 
if (Request.HttpMethod == "POST") 
   SPUtility.ValidateFormDigest(); 

//**************************************** 
// Get a reference the roles that are 
// bound to the current user and the role 
// definition to which we need to verify 
// the user against 
SPRoleDefinitionBindingCollection usersRoles = web.AllRolesForCurrentUser; 
SPRoleDefinitionCollection roleDefinitions = web.RoleDefinitions; 
SPRoleDefinition roleDefinition = roleDefinitions["Full Control"]; 

// Check if the user is in the role. If not 
// redirect the user to the access denied page 
if (usersRoles.Contains(roleDefinition)) 
{ 
   //******************************* 
   //Check if post back to run 
   //code that initiates the page 
   if (IsPostBack != true) 
   { 
    //Do your stuff here 
   } 
} 
else 
{ 
   Response.Redirect("/_layouts/accessdenied.aspx"); 
}

Initial URL
http://blog.rafelo.com/2008/10/programmatically-checking-user-role-or.html

Initial Description

                                

Initial Title
Check if user has been assigned a specific Role Definition

Initial Tags
security, sharepoint

Initial Language
C#