使用 DirectoryServices 从 C# 连接到 LDAP

发布于 2024-08-05 04:24:04 字数 477 浏览 13 评论 0原文

我正在尝试连接到运行 LDAP 的 edirectory v8.8 服务器。我将如何在 .NET 中做到这一点?我是否仍然可以使用 System.DirectoryService 中的类,例如 DirectoryEntryDirectorySearcher 或者它们是 AD 特定的?我需要以不同的方式指定“连接字符串”吗?

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

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

有什么想法吗?

I am trying to connect to an edirectory v8.8 server running LDAP. How would I go about doing that in .NET? Can I still use the classes in System.DirectoryService such as DirectoryEntry and DirectorySearcher or are they AD specific? Do I need to specify the "Connection String" any differently?

I am trying something like the code below but it doesn't seem to work...

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

Any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

娇柔作态 2024-08-12 04:24:04

好吧,我认为您的连接字符串缺少一点 - 仅指定服务器名称还不够好 - 您还需要为您的搜索指定一个“起点”。

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

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

不确定新版本的 eDirectory 与 LDAP 的兼容性如何 - 但这应该可行,因为从理论上讲,它是标准 LDAP,无论实现如何:-)

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

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

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

另请参阅其他 Stackoverflow 问题关于 Novell、LDAP 和 C# - 它可能会为您提供更多信息。

Well, I think your connection string is missing a bit - specifying just the server name isn't good enough - you also need to specify a "starting point" for your search.

In AD, this would typically be something like the "Users" container in your domain, which you'd specify like this in LDAP parlance:

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

Not sure how LDAP compliant the newer versions of eDirectory are - but that should work since in theory, it's standard LDAP regardless of the implementation :-)

But then again: only in theory, there's no difference between theory and practice.....

There's also a System.DirectoryServices.Protocols namespace which offers low-level LDAP calls directly - and that's definitely not tied to AD at all, but it's really quite low-level.....

There's also a Novell C# LDAP library but I've never tried it and can't say how complete or capable it is. It might give you some clues, though!

Also see this other Stackoverflow question about Novell, LDAP and C# - it might give you additional info.

不回头走下去 2024-08-12 04:24:04

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

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

I had a hard time figuring this out but you could use something like the following, it worked sweet for me:

Domain domain = Domain.GetDomain(new DirectoryContext(DirectoryContextType.Domain, "novellBox.sample.com");
DirectorySearcher ds = new DirectorySearcher(domain.GetDirectoryEntry(), searchQuery);
using (SearchResultCollection src = ds.FindAll())
{....}
听风念你 2024-08-12 04:24:04

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

确保您不要忘记使用 using 释放连接 - 如果您不处理目录条目,它们将永远挂在池中,直到池耗尽并且您的应用程序崩溃。

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

I think you need to use LDAP syntax for the host.

Make sure you don't forget to release the connection with using - if you don't dispose of the directory entries they hang around forever until the pool runs out and your app breaks.

using (DirectoryEntry de = new DirectoryEntry ("LDAP://CN=server,DC=domain,DC=com","admin","password",AuthenticationTypes.Secure))
{
    ...
}
流殇 2024-08-12 04:24:04

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

http://mikemstech.blogspot.com/2013/03/搜索-非微软-ldap.html

Depending on the directory server configuration, you might actually need to use the System.DirectoryServices.Protocols namespace. I wrote up a post on connecting to OpenLDAP with it.

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

摘星┃星的人 2024-08-12 04:24:04

如果外部 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) { }

If the external LDAP require authentication with DN try this: first retrieve the DN of user, then try the authentication with DN and user credentials. I've tested it on 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) { }
执着的年纪 2024-08-12 04:24:04

我正在尝试连接到运行 LDAP 的 edirectory 8.8 服务器。我将如何在.Net 中做到这一点?我仍然可以使用 System.DirectoryService 中的类(例如 DirectoryEntry 和 DirectorySearcher)还是它们特定于 AD?

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

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

是的,你是。当传递给 DirectoryEntry 一个以“LDAP://”开头的字符串时,您需要符合 LDAP 语法,这与 URI 语法有很大不同。

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

I am trying to connect to an edirectory 8.8 server running LDAP. How would I go about doing that in .Net? Can I still use the classes in System.DirectoryService such as DirectoryEntry and DirectorySearcher or are they AD specific?

We are using System.DirectoryServices for Microsoft Active Directory, OpenLDAP running on Linux and eDirectiry without any problem. So the answer is yes, you can use these classes to access eDir.

Do I need to specify the "Connection String" any differently?

Yes you are. When passing to DirectoryEntry a string starting with "LDAP://" you need to conform to the LDAP syntax which is very different than URI syntax.

I recommend you to use an LDAP browser (google it, there are many free downloads) in order to get the correct path to the root object otherwise you will spend time on trying to figure out the correct object types.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文