Click here to Skip to main content
15,890,282 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all,

I have a piece of code that returns all the information / properties from Active Directory for a specific user / computer. Can anyone please tell me how do I distinguish between the two (User vs Computer)?

...
                string username = this.textEdit1.Text;
                // create LDAP connection object  
                DirectoryEntry myLdapConnection = CreateDirectoryEntry();

                // create search object which operates on LDAP connection object  
                // and set search object to only find the user specified  
                DirectorySearcher search = new DirectorySearcher(myLdapConnection);
                search.Filter = "(cn=" + username + ")";

                // create results objects from search object  
                SearchResult result = search.FindOne();

                if (result != null)
                {
                    // user exists, cycle through LDAP fields (cn, telephonenumber etc.)  
                    ResultPropertyCollection fields = result.Properties;

                    foreach (String ldapField in fields.PropertyNames)
                    {
                        // cycle through objects in each field e.g. group membership  
                        // (for many fields there will only be one object such as name)  
                        foreach (Object myCollection in fields[ldapField])
                        {
                            Console.WriteLine(String.Format("{0,-20} : {1}", ldapField, myCollection.ToString()));
                        }
                    }
...

        private DirectoryEntry CreateDirectoryEntry()
        {
            // create and return new LDAP connection with desired settings  

            DirectoryEntry ldapConnection = new DirectoryEntry("LDAP://server.local");
            ldapConnection.AuthenticationType = AuthenticationTypes.Secure;

            return ldapConnection;
        }


Many thanks in advance.
Kind regards,
Posted

1 solution

The distinction you need is made in the objectClass property.

When doing a search for a specific class of entries, you need to be more specific when setting the Filter property. This is what we use:
Contacts:  "(&(objectClass=Contact)(objectCategor=Person))"
Employees: "(&(objectClass=User)(company=*))"
Users:     "(objectClass=User)"
Groups:    "(objectClass=Group)"
Computers: "(objectClass=Computer)"


Searching on these filters will return all contacts, users, groups or computers. We manually set the entry's company property to distinguish between actual employee users and non-employee contractors who have access to our network. Use search.FindAll and iterate through the results.

Note that when using more than one criteria (as with Contacts and Employees), you must use prefix notation: the & goes before the two statements being combined.
 
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