【问题标题】:Why does C# DirectoryServices not work with my LDAP server?为什么 C# DirectoryServices 不适用于我的 LDAP 服务器?
【发布时间】:2023-08-15 13:35:01
【问题描述】:

我有一个在生产中运行的 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();
    }
}

【问题讨论】:

    标签: c# active-directory ldap openldap directoryservices


    【解决方案1】:

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

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

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

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

    【讨论】:

    • 嘿,我正在传递适用于生产服务器 ip 的 LDAP://ip,但不是我的。
    最近更新 更多