Click here to Skip to main content
15,112,662 members
Please Sign up or sign in to vote.
4.00/5 (2 votes)
See more:
Hi all,

I stuck in a problem. I had to retrieve the list of user's from active directory using Ldap connection.. I did this job using directory entry. This is how I did:

C#
public static void getUser()
    {
        DirectoryEntry directoryEntry = new DirectoryEntry("WinNT://" + Environment.UserDomainName);
        string userNames = "";
        foreach (DirectoryEntry child in directoryEntry.Children)
        {
            if (child.SchemaClassName == "User")
            {
                userNames += child.Name + Environment.NewLine; //Iterates and binds all user using a newline
            }
        }
        Console.WriteLine("************************Users************************");
        Console.WriteLine(userNames);
    }


Now my question is how we can get the list of user using Ldap Connection.

Any help will be appreciated..
Thanks
Posted

Well, you stopped at google's first result. You should have looked further, like here:
http://stackoverflow.com/questions/5162897/how-can-i-get-a-list-of-users-from-active-directory[^]... but there are dozens of them...
   
Comments
[no name] 12-Jun-12 5:38am
   
Almost I was there.. :)
Anyway thanks for replying...
+5 from my side..
Try this code if it helps.
C#
using (var context = new PrincipalContext(ContextType.Domain, "yourdomain.com"))
{
    using (var searcher = new PrincipalSearcher(new UserPrincipal(context)))
    {
        foreach (var result in searcher.FindAll())
        {
            DirectoryEntry de = result.GetUnderlyingObject() as DirectoryEntry;
            Console.WriteLine("First Name: " + de.Properties["givenName"].Value);
            Console.WriteLine("Last Name : " + de.Properties["sn"].Value);
            Console.WriteLine("SAM account name   : " + de.Properties["samAccountName"].Value);
            Console.WriteLine("User principal name: " + de.Properties["userPrincipalName"].Value);
            Console.WriteLine();
        }
    }
}
Console.ReadLine();
   

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