在(虚拟)服务器上查找 LDAP 域名

Finding LDAP domain name on a (virtual) server

我有一些进程需要当前 LDAP 提供商的域名(基本上是为了从 AD 同步用户信息)。

该过程会提示用户输入源 LDAP 服务器,但会提供有关默认服务器的信息(因此可以只使用默认值)。

以下代码适用于用户工作站,但在服务器上失败:

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

我也试过 "ldap://rootDSE" 但是 NotSupportedException 被抛出:

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

所以,我有几个问题:

  1. 为什么 LOGONSERVER envvar 在服务器上不可用?
  2. 我可以做什么?
  3. 据我所知,RootDSE 是通往真正的 LDAP 服务器的垫脚石吗?

不确定 "LDAP Domain Name" 的含义,但域名可从 LDAP 和 rootDSE 的属性中获得:

defaultNamingContext: DC=YOURDOMAIN,DC=NET

C# 是一个广泛的主题,但我看到 https://msdn.microsoft.com/en-us/library/aa393248%28v=vs.85%29.aspx 显示了对 rootDSE 的访问权限,并特别显示了 defaultNamingContext。

-吉姆

进一步研究 RootDSE 后,我想出了这段代码:

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

它似乎满足了我的需要。

但是,我上面的其余问题仍未得到解答。