Get collection of AD groups associated with AD user

try

{

string domainName = "your domain name";

string usertitle = "Administrator";

List<string> domainControllerAdGroupColl = new List<string>();

using (PrincipalContext pContext = new   PrincipalContext(ContextType.Domain, domainName))

{

using (PrincipalSearchResult<System.DirectoryServices.AccountManagement.Principal> src =  UserPrincipal.FindByIdentity(pContext,          userName).GetGroups(pContext))

{

src.ToList().ForEach(sr => domainControllerAdGroupColl.Add(sr.SamAccountName.ToLower()));

}

}

}

catch { }
Tags:

1 thought on “Get collection of AD groups associated with AD user

Leave a Reply

Your email address will not be published. Required fields are marked *