Using System.DirectoryServices.AccountManagement to find the members of an AD group

A while ago i posted some code that demonstrated how to find the members of an AD group. If you’re using the brandnew .Net 3.5 framework you can take advantage of the System.DirectoryServices.AccountManagement library which is an abstraction for AD (DS, LDS) and SAM accounts. Using this new library my method can be simplified:

private static IEnumerable<string> FindUsernames(string groupname)
{
 PrincipalContext principalContext = new PrincipalContext(ContextType.Domain, "mydomain");
 GroupPrincipal groupPrincipal = GroupPrincipal.FindByIdentity(principalContext, groupname);

 foreach (Principal principal in groupPrincipal.Members)
 {
  yield return principal.DistinguishedName;
 }
}

If you’re looking for more information i would recommend that you read Managing Directory Security Principals in the .NET Framework 3.5.

  1. Gopi Kallepalli

    Thanks for the method. This redcued my code by a lot.

Leave a Comment


NOTE - You can use these HTML tags and attributes:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>