如何使用 DirectoryEntry 成功连接到域为“example.com”的本地主机 LDAP?

问题描述 投票:0回答:0

我在 CLI 中通过 WSL 在 Windows 上运行 Ubuntu,并且我在 Ubuntu 上配置了 LDAP(OpenLDAP:slapd)如下:

BASE    dc=example,dc=com
URI     ldap://localhost

当我在 Ubuntu 上运行以下命令时,它显示了我在 LDAP 服务器上设置的用户和组的结构,这表明我能够使用我的 test 用户连接到服务器:

ldapsearch -D "cn=test,ou=Department,dc=example,dc=com" -w test -b 'dc=example,dc=com' '(objectclass=*)'

在我的 Windows 主机上,我在打开或关闭 Windows 功能 窗口中选中了Active Directory Lightweight Directory Services 框,这允许使用 LDP.exe。在 LDP.exe 中,我还可以连接 LDAP 服务器,方法是选择 Connection > Bind > 为 User 填写“cn=test,ou=Department,dc=example,dc=com” 字段和 Password 字段的“测试” > 并在单击 OK 之前选择 Simple bind

点击OK后,LDP.exe反馈如下:

上面给我的印象是 LDAP 服务器配置正确,但我无法通过我的 C# 代码中的同一用户连接(它运行在同一台 Windows 主机上),我的配置如下:

var de = new DirectoryEntry("LDAP://localhost/OU=Department,DC=example,DC=com", username, password);
var childNames = new List<string>();
foreach(DirectoryEntry child in de.Children)
{
    childNames.Add(child.Name.ToString());
}

每当我运行代码时,我都会在 foreach 部分收到错误消息,提示“用户名或密码不正确。”

我已经尝试将usernamepassword变量设置为我能想到或在网上找到的所有方式,例如将密码设置为“test”或其SSHA哈希,并将username设置为“test "、"cn=test"、"example.com\test" 或 "[email protected]",但我在运行代码时一直收到“用户名或密码不正确”的消息。

我还尝试了多种身份验证形式的用户名和密码组合,例如:

var de = new DirectoryEntry("LDAP://localhost/OU=Department,DC=example,DC=com", username, password, AuthenticationTypes.Secure);

但是我一直找不到正确的配置。如何配置代码以便接受用户名和密码组合?

ldap openldap c#-7.0 directoryentry directorysearcher
© www.soinside.com 2019 - 2024. All rights reserved.