How to get all the AD groups for a particular user?

后端 未结 10 1679
佛祖请我去吃肉
佛祖请我去吃肉 2020-11-28 04:19

I checked this post already. But it doesn\'t answer my question. I want to get all the active directory groups in which a particular user is a member.

I\'ve written

10条回答
  •  感动是毒
    2020-11-28 05:04

    Here is the code that worked for me:

    public ArrayList GetBBGroups(WindowsIdentity identity)
    {
        ArrayList groups = new ArrayList();
    
        try
        {
            String userName = identity.Name;
            int pos = userName.IndexOf(@"\");
            if (pos > 0) userName = userName.Substring(pos + 1);
    
            PrincipalContext domain = new PrincipalContext(ContextType.Domain, "riomc.com");
            UserPrincipal user = UserPrincipal.FindByIdentity(domain, IdentityType.SamAccountName, userName);
    
            DirectoryEntry de = new DirectoryEntry("LDAP://RIOMC.com");
            DirectorySearcher search = new DirectorySearcher(de);
            search.Filter = "(&(objectClass=group)(member=" + user.DistinguishedName + "))";
            search.PropertiesToLoad.Add("samaccountname");
            search.PropertiesToLoad.Add("cn");
    
            String name;
            SearchResultCollection results = search.FindAll();
            foreach (SearchResult result in results)
            {
                name = (String)result.Properties["samaccountname"][0];
                if (String.IsNullOrEmpty(name))
                {
                    name = (String)result.Properties["cn"][0];
                }
                GetGroupsRecursive(groups, de, name);
            }
        }
        catch
        {
            // return an empty list...
        }
    
        return groups;
    }
    
    public void GetGroupsRecursive(ArrayList groups, DirectoryEntry de, String dn)
    {
        DirectorySearcher search = new DirectorySearcher(de);
        search.Filter = "(&(objectClass=group)(|(samaccountname=" + dn + ")(cn=" + dn + ")))";
        search.PropertiesToLoad.Add("memberof");
    
        String group, name;
        SearchResult result = search.FindOne();
        if (result == null) return;
    
        group = @"RIOMC\" + dn;
        if (!groups.Contains(group))
        {
            groups.Add(group);
        }
        if (result.Properties["memberof"].Count == 0) return;
        int equalsIndex, commaIndex;
        foreach (String dn1 in result.Properties["memberof"])
        {
            equalsIndex = dn1.IndexOf("=", 1);
            if (equalsIndex > 0)
            {
                commaIndex = dn1.IndexOf(",", equalsIndex + 1);
                name = dn1.Substring(equalsIndex + 1, commaIndex - equalsIndex - 1);
                GetGroupsRecursive(groups, de, name);
            }
        }
    }
    

    I measured it's performance in a loop of 200 runs against the code that uses the AttributeValuesMultiString recursive method; and it worked 1.3 times faster. It might be so because of our AD settings. Both snippets gave the same result though.

提交回复
热议问题