Check if user has a specific Role Definition


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



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

URL: http://msdn.microsoft.com/en-us/library/dd878359(v=office.12).aspx

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.