使用DirectoryServices从C#连接到LDAP

16

我正在尝试连接到运行LDAP的eDirectory v8.8服务器。我该如何在.NET中实现?我还可以使用 System.DirectoryServices 中的类,例如 DirectoryEntry 和 DirectorySearcher ,或者它们仅适用于AD?我需要以不同的方式指定“连接字符串”吗?

我正在尝试类似下面的代码,但似乎不起作用...

DirectoryEntry de = new DirectoryEntry ("LDAP://novellBox.sample.com","admin","password",AuthenticationTypes.None);
DirectorySearcher ds = new DirectorySearcher(de);
var test = ds.FindAll();

有什么想法吗?

6个回答

14

我认为你的连接字符串还缺少一些内容——仅指定服务器名称是不够的——你还需要为搜索指定一个“起点”。

在AD中,这通常是您域中的“用户”容器,您可以使用LDAP术语指定它:

LDAP://novellBox.sample.com/cn=Users,dc=YourCompany,dc=com

新版本的eDirectory是否符合LDAP协议不确定,但理论上应该可以工作,因为无论如何它都是标准的LDAP实现 :-)

但话说回来:只是在理论上,理论和实践之间是没有区别的......

还有一个System.DirectoryServices.Protocols命名空间,直接提供低级别的LDAP调用 - 这绝对与AD无关,但它确实非常底层......

还有一个Novell C# LDAP library,但我从未尝试过,也不能说它有多完整或有多强大。不过它可能会给你一些线索!

另请参见这个关于Novell、LDAP和C#的Stackoverflow问题 - 它可能会给你额外的信息。


嗨,马克,这个也不行。eDirectory似乎不喜欢它。SEs认为连接字符串中的DC非常适用于AD。我已经看到了其他问题,但是我正在尝试更接近一般的MS实现,而不是依赖于另一个实现。 - Chaitanya
eDir语法很少以dc=this,dc=that结尾。更典型的是,它会使用ou=OrgU,o=Org而不是dc=符号。显然,您必须拥有正确的特定DN用于搜索基础... - geoffc

6
我曾经花了很长时间才弄明白这个问题,但你可以尝试使用以下代码,对我来说非常有效:

我很难弄清楚这个问题,但是你可以尝试使用以下代码,对我来说非常有效:

Domain domain = Domain.GetDomain(new DirectoryContext(DirectoryContextType.Domain, "novellBox.sample.com");
DirectorySearcher ds = new DirectorySearcher(domain.GetDirectoryEntry(), searchQuery);
using (SearchResultCollection src = ds.FindAll())
{....}

嗨,费尔明,这是连接到eDirectory还是AD? “Domain”对象似乎存在于ActiveDirectory命名空间中。尽管仍在努力让它正常工作。 - Chaitanya
搜索查询是什么?例子呢?最终的解决方案是否有完整的源代码示例应用程序? 在我看来,为了尽量减少学习难度,更好的例子是具有完整源代码和良好模式的真实应用程序。 - Kiquenet
searchQuery 是你想要查找的内容。在 MSDN 上有示例:http://msdn.microsoft.com/en-us/library/system.directoryservices.directorysearcher.filter%28v=vs.110%29.aspx。 - Fermin

4

我认为您需要使用LDAP语法指定主机。

确保不要忘记使用using释放连接。如果您不处理目录条目,它们会一直存在,直到池用尽并导致应用程序崩溃。

using (DirectoryEntry de = new DirectoryEntry ("LDAP://CN=server,DC=domain,DC=com","admin","password",AuthenticationTypes.Secure))
{
    ...
}

3

1
如果外部LDAP需要使用DN进行身份验证,请尝试以下方法:首先检索用户的DN,然后尝试使用DN和用户凭据进行身份验证。我已在Domino LDAP上进行了测试。
// Autheticate in external LDAP
string ldapserver = "10.1.1.1:389";
string ldapbasedn = "o=mycompany";
string ldapuser = "cn=Administrator,o=mycompany";
string ldappassword = "adminpassword";
string ldapfilter = "(&(objectclass=person)(cn={0}))";

string user = "usertest";
string password = "userpassword";
try
{
    string DN = "";
    using (DirectoryEntry entry = new DirectoryEntry("LDAP://" + ldapserver + "/" + ldapbasedn, ldapuser, ldappassword, AuthenticationTypes.None))
    {
        DirectorySearcher ds = new DirectorySearcher(entry);
        ds.SearchScope = SearchScope.Subtree;
        ds.Filter = string.Format(ldapfilter, user);
        SearchResult result = ds.FindOne();
        if (result != null )
        {
            DN = result.Path.Replace("LDAP://" + ldapserver + "/" , "");
        }
    }
    // try logon   
    using (DirectoryEntry entry = new DirectoryEntry("LDAP://" + ldapserver + "/" + ldapbasedn, DN, password, AuthenticationTypes.None))
    {
        DirectorySearcher ds = new DirectorySearcher(entry);
        ds.SearchScope = SearchScope.Subtree;
        SearchResult result = ds.FindOne();
    }
} catch (Exception) { }

1
我正在尝试连接到运行LDAP的edirectory 8.8服务器。我该如何在.Net中实现?我还能使用System.DirectoryService中的类,例如DirectoryEntry和DirectorySearcher,还是它们只适用于AD?
我们正在使用System.DirectoryServices来访问Microsoft Active Directory、运行在Linux上的OpenLDAP和eDirectiry,没有任何问题。所以答案是肯定的,你可以使用这些类来访问eDir。
我需要以不同的方式指定“连接字符串”吗?
是的,你需要这样做。当向DirectoryEntry传递以“LDAP://”开头的字符串时,你需要符合LDAP语法,这与URI语法非常不同。
我建议你使用LDAP浏览器(搜索一下,有很多免费下载)来获取正确的根对象路径,否则你将花费时间来尝试找出正确的对象类型。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接