Get List of Active Directory Domains

I recently had a client ask how to retrieve a list of Active Directory Domains as he wanted to give a list to his user. Turns out this is very easy to do in .NET. All you have to do is add a Reference to the System.DirectoryServices DLL and add a using statement:

using System.DirectoryServices.ActiveDirectory;

I typically like to create a class so I can gather information from each Domain object in the DomainCollection and have a set of properties that I can bind to in my applications. For example, in my customer’s application I needed to retrieve the LDAP path. Turns out you can get this path from the Domain object by calling the GetDirectoryEntry() method and retrieving a DirectoryEntry object. You can then get the LDAP path using the Path property. For this sample, I created a class called ADDomain. This class has two properties; Name and Path. Feel free to add additional properties if you need them.

public class ADDomain
{
  public string Name { get; set; }
  public string Path { get; set; }
}

Next you write a simple method to return a List of ADDomain objects. The method, called GetDomains() in this sample will first connect to a domain using the static method GetDomain on the Domain class. This method requires that you pass in the name of one of the domains in your network, and a user id and password that has access to this domain.

Once you have a valid Domain object you can now retrieve the Forest property from this Domain. With the Forest object you can now loop through all of the domains in the Domains collection that is part of the Forest object.

As you loop through each domain you get the information you are interested in and put that data into a new instance of a ADDomain object. This new object is added to your List<ADDomain> collection. Below is the complete method:

public List<ADDomain> GetDomains(string domainController,
  string userName, string password)
{
  Domain domain = null;
  List<ADDomain> ret = new List<ADDomain>();
  Forest forest = null;

  try
  {
    // Connect to Domain
    domain = Domain.GetDomain(
      new DirectoryContext(
          DirectoryContextType.Domain,
          domainController,
          userName,
          password));

    // Get Current Domain Forest
    forest = domain.Forest;

    // Get all Domains in Forest
    foreach (Domain item in forest.Domains)
    {
      ADDomain ad = new ADDomain();

      // Create new class to get the Path
      ad.Name = item.Name;
      ad.Path = item.GetDirectoryEntry().Path;

      ret.Add(ad);
    }
  }
  catch (Exception ex)
  {
    Debug.WriteLine(ex.ToString());
  }

  return ret;
}

After you have built the list of ADDomain objects, return that collection from this method. You can then use that collection to populate any list on any platform such as WPF, Silverlight or ASP.NET.

Summary

In this blog post you learned to get a list of Active Directory (AD) domains by using the classes and methods contained in the System.DirectoryServices.ActiveDirectory namespace. There are many classes contained in this namespace that can be used to do almost anything with AD. In a blog post earlier this year I showed how you can use this namespace along with WPF to create a login screen where users can authenticate against an AD domain. Explore this rich set of classes for your AD tasks.

NOTE: You can download this article and many samples like the one shown in this blog entry at my website. http://www.pdsa.com/downloads. Select “Tips and Tricks”, then “Get List of Active Directory Domains” from the drop down list.

Good Luck with your Coding,
Paul Sheriff

** SPECIAL OFFER FOR MY BLOG READERS **
We frequently offer a FREE gift for readers of my blog. Visit http://www.pdsa.com/Event/Blog for your FREE gift!

Past Blog Content

Blog Archive

No Comments