.Net中的LDAP目录条目 – 不适用于OU =用户

我有以下代码(C#):

(调来自: http ://www.eggheadcafe.com/conversation.aspx?smessidid = 31766061&threadid = 31766050)

DirectorySearcher dseSearcher = new DirectorySearcher(); string rootDSE = dseSearcher.SearchRoot.Path; DirectoryEntry rootDE = new DirectoryEntry(rootDSE); string userDSE = rootDSE.Insert(7, "OU=Users,"); DirectoryEntry userDE = new DirectoryEntry(userDSE); 

rootDSE是正确创建的,但是,如果我尝试使用它,则用户userDSE不可用并抛出“服务器上没有此类对象”exception。

LDAP字符串如下:

Root:LDAP:// DC =公司,DC =本地

用户:LDAP:// OU =用户,DC =公司,DC =本地

我作为管理员在Vista上运行,但也需要在XP(管理员)上工作。

我是LDAP和目录管理的新手,所以我在这里黑暗中磕磕绊绊。 有什么想法吗? 此外 – 任何链接的文章,可以让我了解它如何工作将不胜感激。

我尝试作为测试的第一件事是在创建目录条目时硬编码您想要的路径:

 DirectoryEntry de = new DirectoryEntry("LDAP://OU=Users,DC=company,DC=local"); 

如果这是Active Directory中的实际路径,这将告诉您很快。 我不知道你的AD是什么样的,所以我不能告诉你这是否是一个有效的路径。 在您的Active Directory用户和计算机MMC插件下,如果此路径正确,那么您应该拥有根域,并在根目录下有一个名为Users的OU文件夹。

路径是在AD中向后生成的,因此如果您的“用户”文件夹位于根目录下的另一个OU下,那么它将不会生成

 DirectoryEntry de = new DirectoryEntry("LDAP://OU=Users,OU=,DC=company,DC=local"); 

所以你的AD模式看起来像:

  Root | --> | -->Users 

有关如何在.NET中管理Active Directory的精彩文章:

HowTo:通过C#做(几乎)Active Directory中的所有内容

您可能还想研究.Net 3.5 Framework中提供的System.DirectoryServices,System.DirectoryServices.ActiveDirectory和System.DirectoryServices.AccountManagement命名空间。 我相信System.DirectoryServices和ActiveDirctory名称空间可用于.Net 1.1,而AccountManagement是在.Net 3.5中引入的。

Microsoft文档 – 关于如何使用命名空间的许多好的链接

附录:

要在AD中实际查找用户,您需要执行以下操作:

  DirectoryEntry de = new DirectoryEntry(); de.Path = "LDAP://DC=company,DC=local"; de.AuthenticationType = AuthenticationTypes.Secure; DirectorySearcher deSearch = new DirectorySearcher(); deSearch.SearchRoot = de; deSearch.Filter = "(&(objectClass=user) (cn=" + username + "))"; SearchResult result = deSearch.FindOne(); if (result != null) { DirectoryEntry deUser = new DirectoryEntry(result.Path); ... do what ever you need to the deUser deUser.Close(); } 

这可能看起来很愚蠢,但Active Directory中的默认树设置不是OU = Users ,dc = domain,dc = com,而是cn = Users ,dc = domain,dc = com(注意CN =不是OU =对于用户。

这似乎很愚蠢,因为AD中的容器对象(cn的objectClass)不能是组策略的接收者,但由于我不理解的原因,这是默认的。 (实际上我明白了,这是因为CN的包含更像是NT域而不是OU)

几乎我遇到的每个人,第一次尝试LDAP绑定/ auth到AD。

正如geoffc正确提到的那样,在Active Directory中,域下的“用户”是容器对象而不是组织单元对象。 这会导致完全不同的LDAP路径,这就是您收到错误消息的原因。

如果它修复了您的问题,请尝试以下代码并发布:

 // Replace the "company" and "com" with actual domain values... DirectoryEntry de = new DirectoryEntry("LDAP://CN=Users,DC=company,DC=com"); DirectorySearcher deSearch = new DirectorySearcher(); deSearch.SearchRoot = de; // Set your other search params here