Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check active directory group membership recursively

So I have a question regarding recursive groups in active directory. I have a little method that checks if a user id is in a group or not. Works great. Found out today that it doesn't check recursive group membership and I'm not too sure how (or if) there is a way to do that. Here's what I have so far for non-recursive:

public static bool CheckGroupMembership(string userID, string groupName, string Domain)
{
  bool isMember = false;

  PrincipalContext ADDomain = new PrincipalContext(ContextType.Domain, Domain);
  UserPrincipal user = UserPrincipal.FindByIdentity(ADDomain, userID);

  if (user.IsMemberOf(ADDomain, IdentityType.Name, groupName.Trim()))
  {
    isMember = true;
  }

  return isMember;
}

I've seen some things about a directory searcher or something but I'm somewhat new to working directly with AD and while I understand the concepts, some other things are still a little lost on me.

Thanks!

like image 241
Seril Avatar asked Apr 13 '12 17:04

Seril


People also ask

How to check Active Directory group membership through command line?

Right-click on the domain root and select Find; Enter a username and click Find Now; Open the user properties and go to the Member of tab; This tab lists the groups the selected user is a member of. You can also check Active Directory group membership through command-line. Run the command: net user USERNAME /domain.

How do I see all users in Active Directory?

The easiest and most clear way to get a list of user groups in AD is to use the graphical snap-in Active Directory Users & Computers (ADUC). This tab lists the groups the selected user is a member of. You can also check Active Directory group membership through the command-line.

How to get all ad user groups (recursively) with PowerShell?

How to get ALL AD user groups (recursively) with Powershell or other tools? You can use the LDAP_MATCHING_RULE_IN_CHAIN: You can use it anywahere that you can use an LDAP filter. Show activity on this post. If you make it a function you can call it recursively. Check this out, I think you'll be pleased with the results:

How do I get a list of groups in Aduc?

Getting Group Membership via ADUC. 1 Run the dsa.msc snap-in; 2 Right-click on the domain root and select Find; 3 Enter a username and click Find Now; 4 Open the user properties and go to the Member of tab; 5 This tab lists the groups the selected user is a member of.


2 Answers

You can also check by using the recursive option of GroupPrincipal.GetMembers.

public static bool CheckGroupMembership(string userID, string groupName, string Domain) {
    bool isMember = false;

    PrincipalContext ADDomain = new PrincipalContext(ContextType.Domain, Domain);
    UserPrincipal user = UserPrincipal.FindByIdentity(ADDomain, userID);
    GroupPrincipal group = GroupPrincipal.FindByIdentity(ADDomain, groupName);

    if ((user != null) && (group != null)) {
        isMember = group.GetMembers(true).Contains(user);
    }

    return isMember;
}
like image 159
Steve Young Avatar answered Sep 20 '22 14:09

Steve Young


Here is a solution using System.DirectoryServices.AccountManagement Namespace. It's a kind of recursive solution. In Find Recursive Group Membership (Active Directory) using C#, I give a recursive solution that also works with distribution groups.

/* Retreiving a principal context
 */
Console.WriteLine("Retreiving a principal context");
PrincipalContext domainContext = new PrincipalContext(ContextType.Domain, "WM2008R2ENT:389", "dc=dom,dc=fr", "jpb", "PWD");


/* Look for all the groups a user belongs to
 */
UserPrincipal aUser = UserPrincipal.FindByIdentity(domainContext, "user1");
PrincipalSearchResult<Principal> a =  aUser.GetAuthorizationGroups();

foreach (GroupPrincipal gTmp in a)
{
  Console.WriteLine(gTmp.Name);    
}
like image 28
JPBlanc Avatar answered Sep 19 '22 14:09

JPBlanc