spbsmile
spbsmile

Reputation: 81

how check if CurrentUser is member of group AD?

This code is not suitable:

web.IsCurrentUserMemberOfGroup(web.Groups["Namegruop"].ID);

Upvotes: 1

Views: 2341

Answers (1)

Stefan
Stefan

Reputation: 14880

You need to distinguish between AD security group membership and SharePoint group membership.

In order to check AD security membership you can use System.Security.Principal.WindowsPrincipal.IsInRole. You do not need to use the SharePoint API:

using(WindowsIdentity identity = WindowsIdentity.GetCurrent())
{
  WindowsPrincipal p = new WindowsPrincipal(identity);
  if (p.IsInRole("DOMAIN\\GroupName")) // Alternative overloads with SecurityIdentifier available
  {
    // ...
  } 
}

To check if the current user is member of a SharePoint group you can use the SharePoint API:

SPWeb web = // ...
SPGroup group = web.SiteGroups["GroupName"];
if (group.ContainsCurrentUser)
{
  // ...
}

Upvotes: 3

Related Questions