我正在尝试连接到运行 LDAP 的 edirectory v8.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 中,这通常类似于您域中的“用户”容器,您可以用 LDAP 的说法这样指定:
LDAP://novellBox.sample.com/cn=Users,dc=YourCompany,dc=com
不确定较新版本的 eDirectory 如何与 LDAP 兼容——但这应该可行,因为从理论上讲,无论实现如何,它都是标准的 LDAP :-)
话又说回来了:只是理论上,理论和实践没有区别.....
还有一个
System.DirectoryServices.Protocols
命名空间,它直接提供低级 LDAP 调用 - 绝对与 AD 无关,但它确实非常低级......
还有一个 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
的连接 - 如果您不处理目录条目,它们将永远存在,直到池用完并且您的应用程序中断。
using (DirectoryEntry de = new DirectoryEntry ("LDAP://CN=server,DC=domain,DC=com","admin","password",AuthenticationTypes.Secure))
{
...
}
根据目录服务器配置,您可能实际上需要使用
System.DirectoryServices.Protocols
命名空间。我写了一篇关于用它连接到 OpenLDAP 的文章。
http://mikemstech.blogspot.com/2013/03/searching-non-microsoft-ldap.html
如果外部 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) { }
我正在尝试连接到运行 LDAP 的 edirectory 8.8 服务器。我将如何着手在 .Net 中执行此操作?我是否仍可以使用 System.DirectoryService 中的类,例如 DirectoryEntry 和 DirectorySearcher,或者它们是否特定于 AD?
我们正在使用 System.DirectoryServices for Microsoft Active Directory、运行在 Linux 上的 OpenLDAP 和 eDirectiry,没有任何问题。所以答案是肯定的,您可以使用这些类来访问 eDir。
我需要以任何不同的方式指定“连接字符串”吗?
是的。将以“LDAP://”开头的字符串传递给 DirectoryEntry 时,您需要符合与 URI 语法截然不同的 LDAP 语法。
我建议您使用 LDAP 浏览器(谷歌它,有很多免费下载)以获得根对象的正确路径,否则您将花费时间来尝试找出正确的对象类型。