views:

253

answers:

1

Is there a simple way to resolve the Active Directory path of a Domain name in Active Directory?

E.g. your user account might be SITE\Username or DEPARTMENT\Username but the actual path of the domain SITE might be site.company.com or DEPARTMENT might be dep.company.com etc

I'm trying to find a way of turning DEPARTMENT into DC=dep,DC=company,DC=com etc for searching correctly in Active Directory...

+1  A: 

What you probably have is a company with one forrest (company.com) and a couple of child domains (SITE and DEPARTMENT in your case). The SITE- and DEPARTMENT-bits are the NetBIOS representations of the domains. Is not very common that they differ from the corresponding DNS names but it is possible. Just make sure we're not talking about OU:s and "physical" Active Directory sites.

Assuming the above, here are a couple of options:

The following assumes that your application is running under an account with "forrest-wide read access" (the ability to access your different domains):

using System.DirectoryServices.ActiveDirectory;

// ...

DirectoryContext directoryContext
    = new DirectoryContext(DirectoryContextType.Domain, "DEPARTMENT");
Domain domain = Domain.GetDomain(directoryContext);
String domainName = domain.Name;
String domainComponent = "DC=" + domainName.Replace(".", ",DC=");
Console.WriteLine(domainComponent);

(I haven't found a "System.DirectoryServices-built-in" way to transform domain.company.com to DC=domain,DC=company,DC=com but the simple string manipulation above should do the trick.)

If you're using a non-forrest-wide account (or if DEPARTMENT and SITE are not domains in the same forrest but in themselves separate forrests), then you'll have to maintain a list of usernames and passwords to use depending on the "DEPARTMENT" or "SITE"-strings:

// if(domainNetBios == "DEPARMENT")...
DirectoryContext directoryContext
    = new DirectoryContext(DirectoryContextType.Domain,
    "DEPARTMENT",
    "UserInDEPARTMENT",
    "PassForUserInDEPARTMENT");

If you're not willing to bind to the different forrests/domains to get the domain name/component you could try something along the lines of:

IPAddress[] addresses = Dns.GetHostAddresses("DEPARTMENT");
IPHostEntry host = Dns.GetHostEntry(addresses[0]);
Int32 dotIndex = host.HostName.IndexOf('.');
String domain =
    host.HostName.Substring(dotIndex + 1, host.HostName.Length - dotIndex - 1);
Console.WriteLine(domain);

But the above assumes that the NETBios-name is the same as the first part of the DNS-name and that DNS resolution is working correctly. What we're doing above is to query for a list of domain controllers then removing the hostnames from the DNS-names. Not a particularly clean option...

Per Noalt
brilliant! thanks. A few notes though...Finding domains that are located further away takes a bit longer...I tried using the GlobalCatalog but some of our domains must be DNS representations...not actually in AD...
davidsleeps
also, I was using the exact same replace(".",",DC=") in other parts...has worked fine so far!
davidsleeps