Click here to Skip to main content
16,015,351 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi
I have program which would list the users in the Active Directory.I have created a DirectorySearcher object to which I'm passing the connection string of the domain.

DirectorySearcher dssearch = new DirectorySearcher(connection);

connection value is ldap://example.net.

But as soon as the compiler hits the above line, its not retrieving any values.
Posted

1 solution

C#
static void Main(string[] args)
{
    string groupName = "Domain Users";
    string domainName = "";
 
    PrincipalContext ctx = new PrincipalContext(ContextType.Domain, domainName);
    GroupPrincipal grp = GroupPrincipal.FindByIdentity(ctx, IdentityType.SamAccountName, groupName);
 
    if (grp != null)
    {
         foreach (Principal p in grp.GetMembers(false))
            {
                Console.WriteLine(p.SamAccountName + " - " + p.DisplayName);
            }
 
 
        grp.Dispose();
        ctx.Dispose();
        Console.ReadLine();
    }
    else
    {
        Console.WriteLine("\nWe did not find that group in that domain, perhaps the group resides in a different domain?");
        Console.ReadLine();
    }
}


Original answer : Get list of Active Directory users in C#[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900