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:

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

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

Any help will be appreciated..

Well, you stopped at google's first result. You should have looked further, like here:[^]... but there are dozens of them...
[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.
using (var context = new PrincipalContext(ContextType.Domain, ""))
    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);

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