Finding Active Directory user’s group membership in C#

Hi there,

there are a couple of ways to programmatically find a user and groups he belongs to in Active Directory. Recently I tested a few of them and here are some thoughts of what I found out.


The System.DirectoryServices namespace provides us with a DirectorySearcher class. Filter property of that class can be used in order to specify the search query on the entire directory. An example filter for a user with login name ‘lkarolak’ could look like this:


If search is successful, the FindOne() method of the DirectorySearcher class should return an object of type DirectoryEntry. Finding this object’s membership requires iterating through its properties, finding the ones with name ‘memberOf’, and then (if needed) also performing some recursion in order to find out the nested group membership. After all, a bit complicated and quite resource-costly.


Similar approach could be to use the SearchRequest and SearchResponse objects (this time from System.DirectoryServices.Protocols namespace), which are executed within a LDAP connection. The filter for the query looks just as in the previous example. Also in this case one has to recursively search within the result class’ (SearchResultEntry) attributes in order to get all the user’s nested groups.

Here’s a small example:

// establish a connection to LDAP
LdapDirectoryIdentifier id = new LdapDirectoryIdentifier(domain, port);
LdapConnection _connection = new LdapConnection(id);
_connection.SessionOptions.SecureSocketLayer = secureConnection;
_connection.AuthType = AuthType.Basic;
_connection.Credential = new NetworkCredential(ldapUser, ldapPassword);

// distinguished name of the object 
// at which to start the search.
String _target = "dc=EXAMPLE,dc=COM";

String filter = "(&(objectCategory=person)(SAMAccountName=lkarolak))";
String[] attributesToReturn = { "SAMAccountName", "memberOf", "cn" };

SearchRequest searchRequest = new SearchRequest(_target, filter,
      SearchScope.Subtree, attributesToReturn);
SearchResponse response =

if (response.Entries.Count == 1)
  SearchResultEntry entry = response.Entries[0];
  for (int index = 0; index < entry.Attributes["memberOf"].Count; index++)
      // get the group name, for example:
      String groupName = entry.Attributes["memberOf"][index].ToString();


The most interesting and straightforward solution to me was, however, another approach. Within the namespace System.DirectoryServices.AccountManagement we can find an easy way to find a user in AD and check his group memberships. Without having to recursively loop over the parent groups, we’re able to fetch the groups of the user, an much more data. The only constraint is that the code has to be run on a machine that is located within the domain. Let’s have a look at this sample code:

// "company" is the domain we would like to search in
PrincipalContext pc = new PrincipalContext(ContextType.Domain, "COMPANY");

// get the user of that domain by his username, within the context
UserPrincipal up = UserPrincipal.FindByIdentity(pc, username);

// fetch the group list
PrincipalSearchResult groups = up.GetAuthorizationGroups();
GroupPrincipal[] filteredGroups = (from p in groups
           where p.ContextType == ContextType.Domain
           && p.Guid != null
           && p is GroupPrincipal
           && ((GroupPrincipal)p).GroupScope == GroupScope.Universal
           select p as GroupPrincipal).ToArray();

The last lines actually do the trick. The GetAuthorizationGroups() method would fetch all the security groups of the user. If we would also like to have the distribution groups of the user, we’d have to use the GetGroups() method instead. Of course one could want to filter out some groups, like „Everyone” etc., maybe with help of a LINQ query like here, or in another way.
Anyway, the GroupPrincipal object returned contains the properties we need in order to get the name of the groups of a user (first of all, the DistinguishedName property).

After some unit tests done, it seems also that this last method is the fastest approach of those three mentioned here, probably also due to lack of recursive functions.

Hope this helps,

17 komentarzy do “Finding Active Directory user’s group membership in C#”

  1. Thanks for the using System.DirectoryServices.AccountManagement; example!! Had to figure some part out myself but I finally got it.

  2. PrincipalSearchResult groups = up.GetAuthorization();


    I was using the „memberOf” property but Microsoft seems to have removed that in Windows 2008 Domain Controllers…

  3. Just one question, I’ve tried all of this and more but cannot get any domain local groups from other domains.

    Case in point, I belong to 238 groups in our forest. Our forest has a number of domains based on region so I belong to one Universal group and three domain local groups in the Southwest domain but my account is in the Northwest domain. The code only returns the one Universal group from the Southwest domain.

    I’m told this is by design from Microsoft and have yet to hear of a solution.



  4. Long lasting batteries are also very common and
    they allow the user to continue typing even long after the
    electric connection has been cut. The Android operating-system this phone uses is capable of doing supporting
    several gamers that may play both Mp3 and Mp4 format files.
    You can also view images of towns and cities in a fairly elaborate manner.

  5. Hey guys,

    What do I have to write instead of COMPANY if I want to search the entire directory?

    PrincipalContext pc = new PrincipalContext(ContextType.Domain, „COMPANY”);


      1. It didn’t work with that, but I found a solution, looking like this:

        PrincipalContext pc = new PrincipalContext(ContextType.Domain, “COMPANY:3268”, „DC=company,DC=com”);

  6. There is a faster way to get recursive group membership using the System.DirectoryServices.AccountManagement namespace.

    PrincipalContext domainContext = new PrincipalContext(ContextType.Domain,””);

    GroupPrincipal gp = GroupPrincipal.FindByIdentity(domainContext, „adgroup”);
    UserPrincipal aUser = UserPrincipal.FindByIdentity(domainContext, IdentityType.Name, „user1”);

    bool isingroup = gp.GetMembers(true).Contains(aUser);

    For me, the total time required to get the group principal, the user principal and then use the GetMembers(true) function was under a second whereas using the approach proposed was taking 4 or more seconds more.

    Hope that helps.

Dodaj komentarz

Twój adres e-mail nie zostanie opublikowany.