使用 DirectoryServices 从 C# 连接到 LDAP
我正在尝试连接到运行 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();
有什么想法吗?
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
好吧,我认为您的连接字符串缺少一点 - 仅指定服务器名称还不够好 - 您还需要为您的搜索指定一个“起点”。
在 AD 中,这通常类似于域中的“用户”容器,您可以用 LDAP 术语这样指定:
不确定新版本的 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:
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.
我很难弄清楚这一点,但你可以使用类似下面的东西,它对我来说很有效:
I had a hard time figuring this out but you could use something like the following, it worked sweet for me:
我认为您需要对主机使用 LDAP 语法。
确保您不要忘记使用
using
释放连接 - 如果您不处理目录条目,它们将永远挂在池中,直到池耗尽并且您的应用程序崩溃。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.根据目录服务器配置,您实际上可能需要使用 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
如果外部 LDAP 需要使用 DN 进行身份验证,请尝试以下操作:首先检索用户的 DN,然后尝试使用 DN 和用户凭据进行身份验证。我已经在 Domino LDAP 上测试过它。
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.
我们正在使用 System.DirectoryServices for Microsoft Active Directory、在 Linux 上运行的 OpenLDAP 和 eDirectiry,没有任何问题。所以答案是肯定的,您可以使用这些类来访问 eDir。
是的,你是。当传递给 DirectoryEntry 一个以“LDAP://”开头的字符串时,您需要符合 LDAP 语法,这与 URI 语法有很大不同。
我建议您使用 LDAP 浏览器(google 一下,有很多免费下载)以获得根对象的正确路径,否则您将花费时间尝试找出正确的对象类型。
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.
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.