我正在努力尝试使用 ldap.forumsys.com:389 测试我的 LDAP 身份验证。 服务器详细信息请参见:在线 LDAP 测试服务器
这是我用来从我的 C# 应用程序与服务器通信的代码:
using System.DirectoryServices;
string ldapServerAddress = "ldap.forumsys.com:389";
string ldapBaseDN = "DC=example,DC=com";
string ldapUserName = "CN=read-only-admin,DC=example,DC=com";
string ldapPassword = "password";
try
{
using var directoryEntry = new DirectoryEntry($"LDAP://{ldapServerAddress}/{ldapBaseDN}", ldapUserName, ldapPassword);
directoryEntry.RefreshCache();
}
catch (Exception ex)
{
Console.WriteLine($"Exception caught: {ex.Message}");
}
每次运行此代码时,都会遇到“指定了无效的 dn 语法”。例外。
我已经尝试了许多不同的连接参数变体,例如用大写“DN”代替小写,“[email protected]”而不是“CN=read-only-admin,DC=example,DC=com” “等等。不过似乎没什么用。
我 100% 确定服务器正常工作,我已通过 Apache Directory Studio 连接到它并毫无问题地访问其资源。我也尝试过使用从 ADS 复制的 DN,但这也不起作用。
我做错了什么?
如果我错了请纠正我,但是:
您正在使用
CN=read-only-admin,DC=example,DC=com
作为用户的 DN。
但是,根据 forumsys LDAP 测试服务器文档,只读管理员用户的正确 DN 应该是
uid=read-only-admin,dc=example,dc=com
。
using System.DirectoryServices;
string ldapServerAddress = "ldap.forumsys.com:389";
string ldapBaseDN = "DC=example,DC=com";
string ldapUserName = "uid=read-only-admin,DC=example,DC=com"; // Changed from CN to uid
string ldapPassword = "password";
try
{
using var directoryEntry = new DirectoryEntry($"LDAP://{ldapServerAddress}/{ldapBaseDN}", ldapUserName, ldapPassword);
directoryEntry.RefreshCache();
}
catch (Exception ex)
{
Console.WriteLine($"Exception caught: {ex.Message}");
}