locked
How can we list entries from an OpenLDAP server using DirectoryEntry? RRS feed

  • Question

  • Hello,

    I'm trying to create a c# application to list all the available groups, users and people using DirectoryEntry.

    DirectoryEntry de = new DirectoryEntry("LDAP://LDAP_IP/dc=aa,dc=bbb/", "cn=manager,dc=aa,dc=bbb", "password", AuthenticationTypes.ServerBind);
                DirectoryEntries children = de.Children;
                foreach (DirectoryEntry child in children)
                {
                    Console.WriteLine("Found object '" + child.Name + "'. Schema class name is '" + child.SchemaClassName + "'.");
                }

    With this code I'm able to list headings - Users, People, Manager, Groups.

    How do I list all users, all people, and all groups using DirectoryEntry?

    Monday, February 15, 2016 9:29 AM

Answers

  • Hi SuhasMG,

    >>How do I list all users, all people, and all groups using DirectoryEntry?

    List all users,

    We can accomplish this simply by using the FindAll rather than the FindOne method on our DirectorySearcher object and then iterating through the results.

    using System;
    using System.Text;
    using System.DirectoryServices;
    
    namespace activeDirectoryLdapExamples
    {
       class Program
       {
          static void Main(string[] args)
          {
             Console.Write("Enter property: ");
             String property = Console.ReadLine();
    
             try
             {
                DirectoryEntry myLdapConnection = createDirectoryEntry();
    
                DirectorySearcher search = new DirectorySearcher(myLdapConnection);
                search.PropertiesToLoad.Add("cn");
                search.PropertiesToLoad.Add(property);
    
                SearchResultCollection allUsers = search.FindAll();
    
                foreach(SearchResult result in allUsers)
                {
                   if (result.Properties["cn"].Count > 0 && result.Properties[property].Count > 0)
                   {
                      Console.WriteLine(String.Format("{0,-20} : {1}",
                                    result.Properties["cn"][0].ToString(),
                                    result.Properties[property][0].ToString()));
                   }
                }  
             }
    
             catch (Exception e)
             {
                Console.WriteLine("Exception caught:\n\n" + e.ToString());
             }
          }
    
          static DirectoryEntry createDirectoryEntry()
          {
             // create and return new LDAP connection with desired settings
    
             DirectoryEntry ldapConnection = new DirectoryEntry("rizzo.leeds-art.ac.uk");
             ldapConnection.Path = "LDAP://OU=staffusers,DC=leeds-art,DC=ac,DC=uk";
             ldapConnection.AuthenticationType = AuthenticationTypes.Secure;
             return ldapConnection;
          }
       }
    }
    

    For more information, please also refer to Get List of Active Directory Users in C#

    List all groups;

    If you're on .NET 3.5 or newer, you can use a PrincipalSearcher and a "query-by-example" principal to do your searching:

    // create your domain context
    PrincipalContext ctx = new PrincipalContext(ContextType.Domain);
    
    // define a "query-by-example" principal - here, we search for a GroupPrincipal 
    GroupPrincipal qbeGroup = new GroupPrincipal(ctx);
    
    // create your principal searcher passing in the QBE principal    
    PrincipalSearcher srch = new PrincipalSearcher(qbeGroup);
    
    // find all matches
    foreach(var found in srch.FindAll())
    {
        // do whatever here - "found" is of type "Principal" - it could be user, group, computer.....          
    }

    Or someone provide the following way using  DirectoryEntry , please also have a test

    DirectoryEntry entry = new DirectoryEntry("ldap://ldap.gaurangjadia.com", "scott", "tiger");
    
    DirectorySearcher dSearch = new DirectorySearcher(entry);
    dSearch.Filter = "(&(objectClass=group))";
    dSearch.SearchScope = SearchScope.Subtree;
    
    SearchResultCollection results = dSearch.FindAll();
    
    for (int i = 0; i < results.Count; i++) {
        DirectoryEntry de = results[i].GetDirectoryEntry();
    
        //TODO with "de"
    }

    Best regards,

    Kristin


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    • Proposed as answer by Kristin Xie Monday, February 22, 2016 5:25 AM
    • Marked as answer by Kristin Xie Tuesday, February 23, 2016 1:28 AM
    Tuesday, February 16, 2016 3:42 AM

All replies

  • Hi SuhasMG,

    >>How do I list all users, all people, and all groups using DirectoryEntry?

    List all users,

    We can accomplish this simply by using the FindAll rather than the FindOne method on our DirectorySearcher object and then iterating through the results.

    using System;
    using System.Text;
    using System.DirectoryServices;
    
    namespace activeDirectoryLdapExamples
    {
       class Program
       {
          static void Main(string[] args)
          {
             Console.Write("Enter property: ");
             String property = Console.ReadLine();
    
             try
             {
                DirectoryEntry myLdapConnection = createDirectoryEntry();
    
                DirectorySearcher search = new DirectorySearcher(myLdapConnection);
                search.PropertiesToLoad.Add("cn");
                search.PropertiesToLoad.Add(property);
    
                SearchResultCollection allUsers = search.FindAll();
    
                foreach(SearchResult result in allUsers)
                {
                   if (result.Properties["cn"].Count > 0 && result.Properties[property].Count > 0)
                   {
                      Console.WriteLine(String.Format("{0,-20} : {1}",
                                    result.Properties["cn"][0].ToString(),
                                    result.Properties[property][0].ToString()));
                   }
                }  
             }
    
             catch (Exception e)
             {
                Console.WriteLine("Exception caught:\n\n" + e.ToString());
             }
          }
    
          static DirectoryEntry createDirectoryEntry()
          {
             // create and return new LDAP connection with desired settings
    
             DirectoryEntry ldapConnection = new DirectoryEntry("rizzo.leeds-art.ac.uk");
             ldapConnection.Path = "LDAP://OU=staffusers,DC=leeds-art,DC=ac,DC=uk";
             ldapConnection.AuthenticationType = AuthenticationTypes.Secure;
             return ldapConnection;
          }
       }
    }
    

    For more information, please also refer to Get List of Active Directory Users in C#

    List all groups;

    If you're on .NET 3.5 or newer, you can use a PrincipalSearcher and a "query-by-example" principal to do your searching:

    // create your domain context
    PrincipalContext ctx = new PrincipalContext(ContextType.Domain);
    
    // define a "query-by-example" principal - here, we search for a GroupPrincipal 
    GroupPrincipal qbeGroup = new GroupPrincipal(ctx);
    
    // create your principal searcher passing in the QBE principal    
    PrincipalSearcher srch = new PrincipalSearcher(qbeGroup);
    
    // find all matches
    foreach(var found in srch.FindAll())
    {
        // do whatever here - "found" is of type "Principal" - it could be user, group, computer.....          
    }

    Or someone provide the following way using  DirectoryEntry , please also have a test

    DirectoryEntry entry = new DirectoryEntry("ldap://ldap.gaurangjadia.com", "scott", "tiger");
    
    DirectorySearcher dSearch = new DirectorySearcher(entry);
    dSearch.Filter = "(&(objectClass=group))";
    dSearch.SearchScope = SearchScope.Subtree;
    
    SearchResultCollection results = dSearch.FindAll();
    
    for (int i = 0; i < results.Count; i++) {
        DirectoryEntry de = results[i].GetDirectoryEntry();
    
        //TODO with "de"
    }

    Best regards,

    Kristin


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    • Proposed as answer by Kristin Xie Monday, February 22, 2016 5:25 AM
    • Marked as answer by Kristin Xie Tuesday, February 23, 2016 1:28 AM
    Tuesday, February 16, 2016 3:42 AM
  • How can we get the multiple OUs and OUGuid in that from OpenLDAP from DirectEntry??

    Thanks,

    Pankaj Kadian

    Wednesday, June 19, 2019 10:08 AM