We are querying Active Directory using the DirectoryEntry method by passing a domain name. Under this domain, there are 40 DC's, in that 20 of them are DNS configured, and the rest of them are non-DNS configured, which are not maintained well and not reliable(connecting to these non-DNS configured DC's will usually timeout or thread being aborted).
Now, while making an AD call with directoryEntry method, is there a way to query only the DC's which has the DNS configured?
Currently, the code picks the Non-DNS configured DC.
I know picking the DC in a domain is a domain server task, based on the geographical location and other factors. Is there any way we can modify the code to instruct the DirectoryEntry to pick only the DNS configured DC's when we pass the DomainName.
Sample code in c# .net:
DirectoryEntry obEntry = new DirectoryEntry(@"LDAP://" + DomainName + "/<SID=" + new SecurityIdentifier(groupSid, 0).Value + ">", serviceAccountUser, serviceAccountPassword);
if (obEntry.Guid != null)
{
string distinguishedNameObtained = Convert.ToString(obEntry.Properties["distinguishedName"].Value);
}
You can't tell
DirectoryEntryto pick a subset of DCs, but you can tell it to use one specific DC. In your code, you would set yourDomainNamevariable to the name of the DC:That's the easiest way, but now you've hard-coded one single DC, and if that one goes down, you have to change your code, which isn't ideal.
If you want to chose from the available DCs, you could try using
Domain.GetCurrentDomain()orDomain.GetDomain()(if the computer you're running this from is not on the same domain you're connecting to) and then examining theDomainControllerscollection. I don't know what you mean by the DCs not being configured for DNS, so I'm not sure if that's something you can determine from theDomainControllerclass. Take a look at the documentation forDomainControllerand see if there is something you can use. There is aSiteNameproperty if you want to choose a DC from a specific site.If you are able to do that, then you can use the
Nameproperty of theDomainControllerin your LDAP string.