【问题标题】:How to use LDAP to Query Active Directory on different server如何使用 LDAP 查询不同服务器上的 Active Directory
【发布时间】:2020-09-28 23:18:25
【问题描述】:

我们的域控制器上运行着 Active Directory,另一台服务器上运行着 IIS。我正在开发一个需要查询 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: 提供程序不支持搜索,无法搜索 LDAP://RootDSE。]

显然,我对在多台服务器上使用 LDAP 的理解有所欠缺,感谢您的帮助。

【问题讨论】:

  • Windows 不允许用户名/密码。您需要使用凭据。使用用户名/密码信息未加密。使用凭据,用户名/密码不会在未加密的情况下发送。
  • 你是在本地遇到这个错误,还是只在iis中遇到这个错误?
  • 本地没试过;我正在远程工作,而不是在域中。

标签: c# iis active-directory ldap


【解决方案1】:

您正在获取默认的 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();

【讨论】:

  • 谢谢!做到了。
【解决方案2】:

也许您需要为 IIS 站点关闭匿名身份验证并同时启用 Windows 身份验证?

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-29
    • 2013-01-08
    相关资源
    最近更新 更多