代码之家  ›  专栏  ›  技术社区  ›  Ralph Shillington

属性的DirectoryEntry成员返回完整路径

  •  4
  • Ralph Shillington  · 技术社区  · 15 年前

    我只需要用户所属组的通用名称。

    DirectoryEntry user = new DirectoryEntry("LDAP://cn=myuser....");
    foreach(string path in user.Properties["memberOf"])
        Console.WriteLine(path);
    

    然后memberOf属性包含一组字符串,即组的完整路径。这是有道理的,但这不是我想要的。

    我很确定,我不应该为这些路径中的每一个创建一个新的DirectoryEntry来获得公共名称,但是简单地从路径中解析出cn是最好的方法吗?(这似乎相当残忍)

    必须有更好的方法来获取用户所属组的搜索结果。

    顺便说一句,这是.NET2,所以我不能做任何花哨的LINQtoAD的东西,也不能访问DirectoryServicesforActiveDirectory中的新位。

    3 回复  |  直到 15 年前
        1
  •  2
  •   Tomalak    15 年前

    CN不一定等于集团的名称。不建议从DN中解析它,因为DN是转义的。您需要查询目录中的对象。

    要检索单个对象,请将搜索基础设置为其可分辨名称,将搜索范围设置为“基础”,然后发出查询。

    建议在应用程序中缓存查询结果,以避免多次发出相同的LDAP查询(如果您检索 memberOf 指一行中的多个对象)。

    示例代码( right off the MSDN ,仅稍作修改):

    string dn = "LDAP://CN=Group Name,ON=Groups,DC=fabrikam,DC=com";
    
    // Bind to a specific group.
    DirectoryEntry entry = new DirectoryEntry(dn);
    
    // Create a DirectorySearcher object.
    DirectorySearcher mySearcher = new DirectorySearcher(entry);
    mySearcher.SearchScope = SearchScope.Base;
    mySearcher.PropertiesToLoad.Add("displayName"); 
    
    // Use the FindOne method to find the group object.
    SearchResult resEnt = mySearcher.FindOne();
    
        2
  •  0
  •   Ronald Wildenberg    15 年前

    不幸的是,在.NET2.0中没有比您描述的更好的方法了。这个 memberOf

        3
  •  0
  •   baldpate    10 年前

    在“相关”部分找到此旧线程。


    SearchResult 在一次搜索中。

    所有代码都是C语言。

    DirectoryEntry userEntry = new DirectoryEntry("LDAP://<server>/<user DN>", "user", "pwd");
    
    DirectorySearcher searcher = new DirectorySearcher(userEntry);
    searcher.SearchScope = SearchScope.Base;
    searcher.AttributeScopeQuery = "memberOf";
    searcher.PropertiesToLoad.Clear();
    // just load any attributes you want, not limited to cn
    searcher.PropertiesToLoad.Add("cn");
    
    foreach (SearchResult result in searcher.FindAll())
    {
        Console.WriteLine(result.Path);
    }
    

    限制:

    • 需要2003年的功能级别(忘记域/林)

    LDAP\u匹配\u规则\u链中匹配规则:

    DirectoryEntry rootEntry = new DirectoryEntry("GC://<GC server>", "user", "pwd");
    
    DirectorySearcher searcher = new DirectorySearcher(rootEntry);
    searcher.SearchScope = SearchScope.Subtree;
    searcher.Filter = "(member:1.2.840.113556.1.4.1941:=<user DN>)";
    searcher.PropertiesToLoad.Clear();
    // just load any attributes you want, not limited to cn
    searcher.PropertiesToLoad.Add("cn");
    
    foreach (SearchResult result in searcher.FindAll())
    {
        Console.WriteLine(result.Path);
    }
    

    限制:

    • 不处理主要组成员资格
    • 它获得嵌套的组成员资格,而不仅仅是一个级别的成员资格