Skip to content

Instantly share code, notes, and snippets.

@countnazgul
Last active April 17, 2022 18:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save countnazgul/7d23bf0537e54f8e8edb to your computer and use it in GitHub Desktop.
Save countnazgul/7d23bf0537e54f8e8edb to your computer and use it in GitHub Desktop.
[Get details for Active Directory group] #csharp #asp.net
// add reference to System.DirectoryServices.AccountManagement
// using System.DirectoryServices.AccountManagement;
PrincipalContext ctx = new PrincipalContext(ContextType.Domain);
GroupPrincipal group = GroupPrincipal.FindByIdentity(ctx, "domain\\groupname");
var users = group.GetMembers();
if (group != null)
{
// iterate over members
foreach (Principal p in group.GetMembers())
{
//Console.WriteLine("{0}: {1}", p.StructuralObjectClass, p.DisplayName);
UserPrincipal theUser = p as UserPrincipal;
if (theUser != null)
{
// do something with the user
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment