使用DirectoryServices从C#连接到LDAP

使用DirectoryServices从C#连接到LDAP,c#,ldap,directoryservices,novell,edirectory,C#,Ldap,Directoryservices,Novell,Edirectory,我正在尝试连接到运行LDAP的edirectory 8.8服务器。我将如何在.Net中进行此操作?我仍然可以使用System.DirectoryService中的类,如DirectoryEntry和DirectorySearcher,还是它们是特定于广告的?我是否需要以其他方式指定“连接字符串” 我正在尝试下面的代码,但似乎不起作用 DirectoryEntry de = new DirectoryEntry ("LDAP://novellBox.sample.com","admin","pas

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

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

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

有什么想法吗?

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

确保您不会忘记使用释放与
的连接-如果您不处理目录项,它们会一直挂在那里,直到池用完并且您的应用程序中断

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

我很难弄明白这一点,但你可以用下面这样的方法,它对我很有用:

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

嗯,我认为您的连接字符串丢失了一点—仅指定服务器名称是不够的—您还需要为搜索指定一个“起点”

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

LDAP://novellBox.sample.com/cn=Users,dc=YourCompany,dc=com
不确定较新版本的eDirectory与LDAP的兼容性如何-但这应该可以工作,因为从理论上讲,无论实现如何,它都是标准LDAP:-)

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

还有一个
System.DirectoryServices.Protocols
名称空间,它直接提供低级LDAP调用,这与AD毫无关系,但它的级别非常低

还有一个问题,但我从未尝试过,也说不出它有多完整或功能。不过,它可能会给你一些线索

另请参阅关于Novell、LDAP和C#的另一篇文章,它可能会为您提供更多信息

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

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

我是否需要以其他方式指定“连接字符串”

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


我建议您使用LDAP浏览器(google it,有许多免费下载),以获得根对象的正确路径,否则您将花费时间尝试找出正确的对象类型。

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


如果外部LDAP需要使用DN进行身份验证,请尝试以下操作:首先检索用户的DN,然后尝试使用DN和用户凭据进行身份验证。我已经在DominoLDAP上测试了它

// 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) { }

嗨Fermin,这是连接到edirectory还是AD?“域”对象似乎位于ActiveDirectory命名空间中。但仍在努力让它工作。什么是searchQuery?样品。有完整源代码示例应用程序的最终解决方案吗?IMHO,最小化学习曲线的更好示例是具有完整源代码和良好模式的真实应用程序。searchQuery是您试图找到的任何东西。样本可在MSDN上获得:。嗨,马克,也不适用于此。eDirectory似乎不喜欢它。SEs认为连接字符串中的DC非常特定于AD。我已经看到了另一个问题,但我试图更接近一般的MS实现,而不是依赖另一个实现。eDir语法很少以dc=this,dc=that结尾。更典型的是,它将是ou=OrgU,o=Org,而不是dc=notation。显然,您必须为搜索库提供正确的特定DN。。。