【问题标题】:DirectoryEntry memberOf property returns full pathDirectoryEntry memberOf 属性返回完整路径
【发布时间】:2014-11-25 18:06:54
【问题描述】:

我只需要用户所属组的 commonName。

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

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

我很确定我不应该为每个路径新建一个 DirectoryEntry 来获取通用名称,但是从路径中简单地解析出 cn 是不是最好的主意? (这似乎相当野蛮)

必须有更好的方法来获取用户所属组的 SearchResults。

顺便说一句,这是 .NET 2,所以我不能做任何花哨的 LINQ to AD 东西,也不能访问 ActiveDirectory 的 DirectoryServices 中的新位。

【问题讨论】:

    标签: active-directory ldap


    【解决方案1】:

    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】:

      不幸的是,.NET 2.0 中没有比您描述的更好的方法了。 memberOf 属性仅包含用户所属的所有组的完整专有名称,因此您最好的解决方案是解析每个专有名称。

      【讨论】:

        【解决方案3】:

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

        对于这个问题还有另外两个建议。
        他们每个人都可以在一次搜索中直接以SearchResult获取memberOf属性中的对象。

        所有代码都在 C# 中。

        属性范围查询 (ASQ):

        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 的功能级别(忘记域/森林)
        • ASQ 不能跨域工作(至少 System.DirectoryServices 不能,它会为另一个域中的任何对象抛出异常)

        LDAP_MATCHING_RULE_IN_CHAIN 匹配规则:

        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);
        }
        

        限制:

        • 不处理主要组成员身份
        • 需要 2008 R2 的功能级别(忘记域/森林)
        • 它获得嵌套的组成员身份,而不仅仅是一级 memberOf

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2013-07-08
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2013-03-06
          • 2022-08-23
          • 1970-01-01
          相关资源
          最近更新 更多