Last active
April 17, 2022 18:53
-
-
Save countnazgul/7d23bf0537e54f8e8edb to your computer and use it in GitHub Desktop.
[Get details for Active Directory group] #csharp #asp.net
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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