使用DirectoryServices从C#连接到LDAP

我正在尝试连接到运行LDAP的edirectory 8.8服务器。 我将如何在.Net中进行此操作? 我是否仍然可以使用System.DirectoryService中的类,例如DirectoryEntry和DirectorySearcher,或者它们是否特定于AD? 我是否需要以不同方式指定“连接字符串”?

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

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

有任何想法吗?

好吧,我认为你的连接字符串有点缺失 – 只指定服务器名称不够好 – 你还需要为搜索指定一个“起点”。

在AD中,这通常类似于域中的“Users”容器,您在LDAP用语中指定了这样的容器:

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

不确定新版eDirectory的LDAP兼容性 – 但这应该有效,因为理论上它是标准LDAP,无论实现如何:-)

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

还有一个System.DirectoryServices.Protocols命名空间,它直接提供低级别的LDAP调用 – 而且它绝对不会与A​​D绑定,但它确实非常低级……

还有一个Novell C#LDAP库,但我从未尝试过,也不能说它是完整的还是有能力的。 不过,它可能会给你一些线索!

另请参阅有关Novell,LDAP和C#的其他Stackoverflow问题 – 它可能会为您提供其他信息。

我很难搞清楚这一点,但你可以使用类似下面的东西,它对我很有用:

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

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

确保您不要忘记使用以下内容释放连接 – 如果您不处理目录条目,它们将永远挂起,直到池用完并且您的应用程序中断。

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

根据目录服务器配置,您实际上可能需要使用System.DirectoryServices.Protocols命名空间。 我写了一篇关于用它连接到OpenLDAP的post。

http://mikemstech.blogspot.com/2013/03/searching-non-microsoft-ldap.html

如果外部LDAP要求使用DN进行身份validation,请尝试以下操作:首先检索用户的DN,然后尝试使用DN和用户凭据进行身份validation。 我在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) { } 

我正在尝试连接到运行LDAP的edirectory 8.8服务器。 我将如何在.Net中进行此操作? 我是否仍然可以使用System.DirectoryService中的类,例如DirectoryEntry和DirectorySearcher,或者它们是否特定于AD?

我们正在使用System.DirectoryServices for Microsoft Active Directory,在Linux上运行的OpenLDAP和eDirectiry没有任何问题。 所以答案是肯定的,您可以使用这些类来访问eDir。

我是否需要以不同方式指定“连接字符串”?

是的,你是。 向DirectoryEntry传递一个以“LDAP://”开头的字符串时,您需要遵循与URI语法非常不同的LDAP语法。

我建议你使用LDAP浏览器(谷歌它,有很多免费下载),以获得根对象的正确路径,否则你将花时间试图找出正确的对象类型。