【问题标题】:LDAP Directory Entry in .Net - not working with OU=Users.Net 中的 LDAP 目录条目 - 不适用于 OU=Users
【发布时间】:2009-09-10 12:31:36
【问题描述】:

我有以下代码(C#):

(调整自:http://www.eggheadcafe.com/conversation.aspx?messageid=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 无法使用,如果我尝试使用它,则会引发“服务器上没有此类对象”异常。

LDAP 字符串如下:

根目录:LDAP://DC=company,DC=local

用户:LDAP://OU=Users,DC=company,DC=local

我以管理员身份在 Vista 上运行,但也需要它在 XP(管理员)上运行。

我是 LDAP 和目录管理的新手,所以我在这里摸不着头脑。有什么想法吗?另外 - 任何可以链接的文章也可以让我了解它是如何工作的。

【问题讨论】:

    标签: c# .net ldap


    【解决方案1】:

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

    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=<first OU folder>,DC=company,DC=local");
    

    所以您的 AD 架构如下所示:

     Root 
     |
     --><first OU folder>
         |
         -->Users
    

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

    HowTo: Do (Almost) Everything in Active Directory via C#

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

    Microsoft Documentation - A lot of good links on how to use the namespace

    附录:

    要真正在 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();
     }
    

    【讨论】:

    • 嗯,谢谢你的链接,最有用的。对于这个项目,我仅限于 .Net 2.0。此外,我希望在安装了我的应用程序的任何机器上创建本地用户,因此用户组可能会有所不同。有没有简单的方法来获取标准用户组?干杯
    • 另外 - 我从哪里获得 UserId?好像是随机扔进我提供的链接里的示例代码里的,不知道应该是什么。
    • 如果您希望创建本地用户,那么您将不会使用 Active Directory,而是需要使用稍微复杂一些的 Win32 API。请参阅codeproject.com/KB/cs/groupandmembers.aspx 至于您的第二条评论,OU=Users 不是用户 ID,OU 是缩写或组织单位,如果您要查找要使用的用户 CN=,CN 是 Common 的缩写名称和您需要用来过滤用户的内容。有关如何在 AD 中查询用户,请参阅 stackoverflow.com/questions/825237/…
    • 查看我的帖子的附录,这将允许您从根目录中实际搜索 AD 中的用户,它应该找到过滤器分配中提供的用户名为的任何用户,只需硬编码或传入您要查找的用户。
    【解决方案2】:

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

    这似乎很愚蠢,因为 AD 中的容器对象(cn 的 objectClass)不能成为组策略的接收者,但由于我不明白的原因,这是默认设置。 (其实我明白,这是因为CN的包含比OU更类似于NT域)

    让我遇到的几乎每个人,第一次尝试将 LDAP 绑定/身份验证到 AD。

    【讨论】:

    • 感谢 CN=Users,OU=users 无法正常工作,我正在寻找宝贵的帮助
    【解决方案3】:

    正如 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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-12-30
      • 2015-11-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多