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

C# 如何使用LDAP查询不同服务器上的Active Directory,c#,iis,active-directory,ldap,C#,Iis,Active Directory,Ldap,我们在域控制器上运行Active Directory,另一台服务器运行IIS。我正在开发一个需要查询广告的web应用程序 页面加载代码在最后一行失败: string Iam; string myLDAP; DirectoryEntry de = new DirectoryEntry("LDAP://RootDSE"); myLDAP = "LDAP://" + de.Properties["defaultNamingContext"]

我们在域控制器上运行Active Directory,另一台服务器运行IIS。我正在开发一个需要查询广告的web应用程序

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

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:提供程序不支持搜索,并且无法搜索LDAP://RootDSE。]


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

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

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

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

请尝试以下代码:

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();

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

请尝试以下代码:

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();

Windows不允许用户名/密码。您需要使用凭据。使用用户名/密码,信息未加密。使用凭据时,不会发送未加密的用户名/密码。您是在本地遇到此错误,还是仅在iis中遇到此错误?尚未在本地尝试;我正在远程工作,而不是在域上。Windows不允许用户名/密码。您需要使用凭据。使用用户名/密码,信息未加密。使用凭据时,不会发送未加密的用户名/密码。您是在本地遇到此错误,还是仅在iis中遇到此错误?尚未在本地尝试;我正在远程工作,而不是在域上。谢谢!成功了,谢谢!成功了。