Get collection of AD groups associated with AD user

  • Posted by: Softree Technology
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 { }
Author: Softree Technology

Leave a Reply

1 Comment