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:
LOGONSERVER
envvar unavailable on servers?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.