如何使用 LDAP 查询不同服务器上的 Active Directory

How to use LDAP to Query Active Directory on different server

我们的域控制器和另一台服务器 运行 IIS 上有 Active Directory 运行。我正在开发一个需要查询 AD 的网络应用程序。

页面加载代码在最后一行失败:

string Iam;
string myLDAP;

DirectoryEntry de = new DirectoryEntry("LDAP://RootDSE");
myLDAP = "LDAP://" + de.Properties["defaultNamingContext"][0].ToString();
TextBox1.Text = "Retrieving your security details.....";

Iam = HttpContext.Current.User.Identity.Name;
TextBox1.Text += " " + Iam + " " + myLDAP;

DirectorySearcher ds = new DirectorySearcher(de);
ds.Filter = "(&(objectCategory=user)(objectClass=person))";

SearchResultCollection result = ds.FindAll();

我收到这个错误:

[NotSupportedException: The provider does not support searching and cannot search LDAP://RootDSE.]

很明显,我对在多个服务器上使用 LDAP 的理解遗漏了一些东西,感谢帮助。

也许您需要为 IIS 站点设置匿名身份验证并同时启用 windows 身份验证?

您正在获取默认的 LDAP 命名上下文 - 但您没有 使用 它 - 您需要根据来自的结果创建一个新的 DirectoryEntry LDAP://RootDSE 对象,然后在默认命名上下文的范围内搜索。

试试这个代码:

string myLDAP;

DirectoryEntry de = new DirectoryEntry("LDAP://RootDSE");
myLDAP = "LDAP://" + de.Properties["defaultNamingContext"][0].ToString();

// define a new DirectoryEntry based on the "defaultNamingContext"
DirectryEntry deMyLdap = new DirectoryEntry(myLDAP);

// now search based on *THAT* scope - not the "RootDSE" scope...
DirectorySearcher ds = new DirectorySearcher(deMyLdap);
ds.Filter = "(&(objectCategory=user)(objectClass=person))";

SearchResultCollection result = ds.FindAll();