Finding LDAP domain name on a (virtual) server

652 views Asked by At

I've got some processes that require the domain name of the current LDAP provider (basically, to synchronize user info from AD).

The process prompts the user for the source LDAP server, but provides info on the default one (so one can just use the default value).

The following code works for user workstations, but fails on servers:

var uri = "LDAP://" + Environment.GetEnvironmentVariable("LOGONSERVER");

I also tried "ldap://rootDSE" but a NotSupportedException was thrown:

The provider does not support searching and cannot search LDAP://rootDSE.

So, I've got a few questions:

  1. Why is LOGONSERVER envvar unavailable on servers?
  2. What can I do instead?
  3. As I take it, RootDSE is a stepping stone towards the real LDAP server?
2

There are 2 answers

0
Christian On BEST ANSWER

After further investigating RootDSE, I came up with this code:

using (DirectoryEntry rootDSE = new DirectoryEntry("LDAP://RootDSE"))
{
    result = (rootDSE.Properties["dnsHostName"].Value ?? "").ToString();
    if (result != "") return result;
}

It seems to do what I need.

However, the rest of my questions above remain unanswered.

0
jwilleke On

Not sure what you imply by "LDAP Domain Name", but the Domain Name is available from LDAP the rootDSE from the attribute:

defaultNamingContext: DC=YOURDOMAIN,DC=NET

C# is a broad subject but I see https://msdn.microsoft.com/en-us/library/aa393248%28v=vs.85%29.aspx shows access to the rootDSE and specifically shows the defaultNamingContext.

-jim