Using DirectorySearcher : Active Directory « Web Services « C# / C Sharp






Using DirectorySearcher

 

using System;
using System.Net;
using System.DirectoryServices;


public class MainClass {
    public static void Main() {
        using (DirectoryEntry de = new DirectoryEntry("LDAP://yourV/OU=yourV, DC=explorer, DC=local"))
        using (DirectorySearcher searcher = new DirectorySearcher()) {
            de.Username = @"explorer\yourName";
            de.Password = "password";
            searcher.SearchRoot = de;
            searcher.Filter = "(&(objectClass=user)(description=Auth*))";
            searcher.SearchScope = System.DirectoryServices.SearchScope.Subtree;
            searcher.PropertiesToLoad.Add("name");
            searcher.PropertiesToLoad.Add("description");
            searcher.PropertiesToLoad.Add("givenName");
            searcher.PropertiesToLoad.Add("wWWHomePage");
            searcher.Sort = new SortOption("givenName", SortDirection.Ascending);
            SearchResultCollection results = searcher.FindAll();
            foreach (SearchResult result in results) {
                ResultPropertyCollection props = result.Properties;
                foreach (string propName in props.PropertyNames) {
                    Console.Write(propName + ": ");
                    Console.WriteLine(props[propName][0]);
                }
            }
        }
    }
}

 








Related examples in the same category

1.DirectoryServices DirectoryEntry
2.retrieves Active Directory information
3.DirectoryEntry Get PropertiesDirectoryEntry Get Properties
4.DirectoryEntry : List Objects
5.DirectoryServices:Modify Property
6.DirectoryEntry and DirectoryEntries
7.DirectoryEntry Rename Object
8.DirectoryServices: Simple Search
9.DirectoryServices: Add Object
10.DirectoryServices: Add Property
11.DirectoryServices Bind ObjectDirectoryServices Bind Object
12.DirectoryServices: Delete Object
13.DirectoryServices GUI