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

7 .net c# ldap

我有以下代码(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);
Run Code Online (Sandbox Code Playgroud)

rootDSE是正确创建的,但是,userDSE如果我尝试使用它,则用户无法使用并抛出"服务器上没有此类对象"异常.

LDAP字符串如下:

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

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

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

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

Sco*_*nce 13

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

DirectoryEntry de = new DirectoryEntry("LDAP://OU=Users,DC=company,DC=local");
Run Code Online (Sandbox Code Playgroud)

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

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

DirectoryEntry de = new DirectoryEntry("LDAP://OU=Users,OU=<first OU folder>,DC=company,DC=local");
Run Code Online (Sandbox Code Playgroud)

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

 Root 
 |
 --><first OU folder>
     |
     -->Users
Run Code Online (Sandbox Code Playgroud)

有关如何在.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();
 }
Run Code Online (Sandbox Code Playgroud)

  • 如果您要创建本地用户,那么您将不会使用Active Directory,而是需要使用Win32 API,这有点复杂.请参阅http://www.codeproject.com/KB/cs/groupandmembers.aspx至于您的第二条评论,如果您要查找您想要的用户,则OU =用户不是用户ID,OU是短或组织单位要使用CN = <用户名>,CN是Common Name的缩写,以及您需要用来过滤用户的内容.有关如何在AD中查询用户的信息,请参阅http://stackoverflow.com/questions/825237/how-can-you-find-a-user-in-active-directory-from-c/825347#825347. (2认同)

geo*_*ffc 6

这可能看起来很愚蠢,但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.