CorvettePower.COM
12Apr/05

Searching all of Active Directory via C# .NET

I've been pouring over ways to search AD for properties, and users. And had resulted into querying each tree looking for items. By moving to a model where you determine the GC, and query it. It searches in all trees in the forest.


DirectoryEntry entry = new DirectoryEntry("GC:");
IEnumerator ie = entry.Children.GetEnumerator();
ie.MoveNext();
entry = (DirectoryEntry)ie.Current;
searcher = new DirectorySearcher(entry);
searcher.Filter = " ((proxyAddresses=" + smtpProxy + "))";

With the value of smtpProxy being something like "smtp:username*" will return all objects in the forest that have email proxyAddresses that start with that username. Very usefull when trying to quickly determine if a user already is using an email alias before adding it (since AD will let you add duplicates of this property).

Userfull Links.

Using System.DirectoryServices to Search the Active Directory

DirectorySearcher.SearchRoot Property

Google Search Words: GC: DirectorySearcher DirectoryEntry

Things to add to your .NET project

using System.DirectoryServices;
using System.Collections;

Trackbacks are disabled.