【问题标题】:ldap nested group membershipldap 嵌套组成员资格
【发布时间】:2012-09-25 12:32:28
【问题描述】:

我的用户是“SPR”,它位于 dc=aaaldap,dc=com 下

现在我要发送的过滤器是(想法:提取用户 SPR 所属的所有组) 过滤器:

(&(objectclass=*)(memberof:1.2.840.113556.1.4.1941:=cn=SPR,dc=aaaldap,dc=com))

作为此搜索结果的一部分,我从 AD 服务器获得了 ldapsearchresref 的响应(据我了解,这是来自 ldap 服务器的指示,它无法在其服务器中找到条目,因此提供了对 URL 的引用可能有助于解析条目的另一台服务器)。

我的疑问是为什么它无法找到我确定条目确实存在的任何条目?

另外,其次,我在某处读到 LDAP 搜索过滤器不适用于逗号。有人可以帮我解决这个问题吗?

【问题讨论】:

  • 为什么不使用 PrincipalContextPrincipalSearchResult 进行查询?
  • 嗨,Rahul,您能详细说明一下吗?

标签: active-directory ldap


【解决方案1】:

要喜欢用户所属的所有组,包括嵌套组,您需要在组中搜索成员属性:

(member:1.2.840.113556.1.4.1941:=(cn=SPR,dc=aaaldap,dc=com))

-吉姆

【讨论】:

    【解决方案2】:

    如果您需要查找该用户所属的所有组,可以使用 PrincipalContext

    让我告诉你怎么做

    PrincipalContext pr = new PrincipalContext(ContextType.Domain, "aaaldap.com", "dc=aaaldap,dc=com", username, password);
    List<string> lst = new List<string>();
    UserPrincipal user = UserPrincipal.FindByIdentity(pr, DomainId);
    if (user != null)
      {
        PrincipalSearchResult<Principal> results = user.GetGroups();
    
       foreach (Principal p in results)
        {
          lst.Add(p.ToString());
        }
      lst.OrderBy(item => item.ToString());
        }
      pr.Dispose();
     return lst;
    

    我猜这就是你要找的东西。

    干杯

    【讨论】:

    • 此方法找不到嵌套组。来自 MSDN:此重载方法仅返回主体直接为其成员的组;不执行递归搜索。 msdn.microsoft.com/en-us/library/… 可能使用 GetAuthorizationGroups?
    【解决方案3】:

    使用 LDAP 和查询时,您有时会获得一个引用 URL,这意味着该帐户是已知的,但位于不同的域中。当我查询我们的全局目录时会发生这种情况,所以我不再这样做了。 :)

    这适用于我们这里的域。请注意,我的过滤器中有逗号。

        private static void showMemberships()
        {
                            // instantiate the DirectoryEntry instance with the FQDN of the domain to connect to
                DirectoryEntry directoryObject = new DirectoryEntry("LDAP://CHILD.DOMAIN.ORG");
    
                // create a DirectorySearcher object and pass the DirectoryEntry instance
                DirectorySearcher ds = new DirectorySearcher(directoryObject);
    
                // set search filter using LDAP query format
                // this example fetches members for a group limiting to users only (not groups) 
                // and where the users are not in the stale objects ou
                ds.Filter = "(&(objectCategory=User)(!ou=Stale Objects)(memberOf=CN=GROUPNAME,CN=Users,DC=CHILD,DC=DOMAIN,DC=ORG))";
    
                // perform the search using the filter and store in a SearchResultsCollection
                SearchResultCollection results = ds.FindAll();
    
                // iterate through the results and do something with the info
                foreach (SearchResult current in results)
                {
                    string userId = current.Properties["cn"][0].ToString().Trim().ToUpper();
                    string userDn = current.Properties["distinguishedName"][0].ToString().Trim().ToUpper();
    
                    Console.Write(userId + " (" + userDn + ")\n");
                }
    
                // set the resource instances as released
                directoryObject.Close();
                directoryObject.Dispose();
        }
    

    【讨论】: