Check if user has been assigned a specific Role Definition


/ Published in: C#
Save to your folder(s)



Copy this code and paste it in your HTML
  1. SPWeb web = SPContext.Current.Web;
  2.  
  3. //****************************************
  4. // Validate the page request to avoid
  5. // any malicious posts
  6. if (Request.HttpMethod == "POST")
  7. SPUtility.ValidateFormDigest();
  8.  
  9. //****************************************
  10. // Get a reference the roles that are
  11. // bound to the current user and the role
  12. // definition to which we need to verify
  13. // the user against
  14. SPRoleDefinitionBindingCollection usersRoles = web.AllRolesForCurrentUser;
  15. SPRoleDefinitionCollection roleDefinitions = web.RoleDefinitions;
  16. SPRoleDefinition roleDefinition = roleDefinitions["Full Control"];
  17.  
  18. // Check if the user is in the role. If not
  19. // redirect the user to the access denied page
  20. if (usersRoles.Contains(roleDefinition))
  21. {
  22. //*******************************
  23. //Check if post back to run
  24. //code that initiates the page
  25. if (IsPostBack != true)
  26. {
  27. //Do your stuff here
  28. }
  29. }
  30. else
  31. {
  32. Response.Redirect("/_layouts/accessdenied.aspx");
  33. }

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

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.