Get all Groups with all users from ActiveDirectory

  • Thread starter Thread starter CodeCase
  • Start date Start date
C

CodeCase

Guest
Hi,

Im trying to query the ad to get all groups and all members in the groups. so far I can use my function to get gruops by string username, thats okay for me, except that I need that for all Users in my Domain.

string[] output = null;

string username = "username";

using (var ctx = new PrincipalContext(ContextType.Domain))
using (var user = UserPrincipal.FindByIdentity(ctx, username))
{
if (user != null)
{
output = user.GetGroups() //this returns a collection of principal objects
.Select(x => x.Name) // select the Sid. you may change this to choose the display name or whatever you want
.ToArray(); // convert to string array

foreach (var res in output)
{

try
{
Console.WriteLine("Group" + res + " has Member " + username + ");
}
catch
{
Console.WriteLine("Group and Person relation not found");
Console.WriteLine();
await session.CloseAsync();
}

};
await driver.CloseAsync();

}
}


I think I have to start with something like:


using (PrincipalSearcher searcher = new PrincipalSearcher(new UserPrincipal(context)))

foreach (var result in searcher.FindAll())
{
DirectoryEntry de = result.GetUnderlyingObject() as DirectoryEntry;


username = de.[samAccountName].value;


}




but then im lost...

Hope someone could help me to resolve that.

Continue reading...
 
Back
Top