为什么 C# 目录服务不适用于我的 LDAP 服务器



我有一个在生产环境中运行的 AD/LDAP 服务器,运行良好。我需要测试东西,所以我使用 phpldapadmin 在本地创建了自己的。

当我尝试对objectClass进行通配符搜索时,我的代码会抛出。这只发生在我的本地服务器上。

System.DirectoryServices.DirectoryServicesCOMException:"指定了无效的 dn 语法。

我尝试了多种DirectoryServices支持的用户名语法:它们都在生产服务器上工作,在我的本地服务器上失败。我可以成功地进入并使用JXplorer导航我的本地服务器。

class Program
{
static void Run(string ip, string username, string password)
{
var authType = System.DirectoryServices.AuthenticationTypes.None;
var directoryEntry = new DirectoryEntry(ip, username, password, authType);
var directorySearcher = new DirectorySearcher(directoryEntry, "(objectClass=*)");
directorySearcher.SearchScope = SearchScope.OneLevel;
// throws here
var searchResult = directorySearcher.FindOne();
}
static void Main(string[] args)
{
Run("LDAP://validlocalip", "admin@test", "test");
Console.ReadKey();
}
}

它不喜欢您作为path参数传递给DirectoryEntry构造函数的内容:

var directoryEntry = new DirectoryEntry(ip, username, password, authType);

该参数是 LDAP 路径,而不仅仅是 IP。如果您只有一个IP,那么您可以尝试"LDAP://ip",但通常您会使用您的域名:"LDAP://domain.com">

更多信息在这里: https://learn.microsoft.com/en-ca/dotnet/api/system.directoryservices.directoryentry.path

最新更新