ldap查询以查找安全组中的所有计算机

时间:2011-11-04 09:51:58

标签: c# ldap

这是我尝试的:

public List<string> GetUsersInGroup(string domain, string group)
{
   List<string> groupMemebers = new List<string>();    
   DirectoryEntry entry = new DirectoryEntry("LDAP://" + domain);                
   DirectorySearcher groupSearch = new DirectorySearcher(entry);
   groupSearch.Filter = "(&(objectclass=group)(samaccountname=" + group +"))";
   groupSearch.PropertiesToLoad.Add("DistinguishedName");
   SearchResult srG = groupSearch.FindOne();    
   String DN = srG.Properties["DistinguishedName"][0].ToString();    
   entry.RefreshCache(new string[] { "memberOf" });    
   DirectorySearcher mySearcher = new DirectorySearcher(entry);
   mySearcher.Filter = "(|(&(objectClass=computer)(memberOf=" + DN + "))(&(objectClass=User)(memberOf=" + DN + ")))";

   SearchResultCollection srcg = mySearcher.FindAll();

   foreach (SearchResult resEnt in srcg)
   {
       groupMemebers.Add(resEnt.GetDirectoryEntry().Name.ToString());
   }

   return groupMemebers;
}

修改

最好找到USERS属于该组,但如果组是“域计算机”或“域控制器”(主要组!),我可以让计算机(memberOF)成为组。

有人可以帮助我吗?

1 个答案:

答案 0 :(得分:3)

AD中对象(用户或计算机)的主要组存储在名为“primaryGroupID”的属性中。例如,对于用户而言,这通常是513,这意味着主要组是“域用户”。

内置组(域用户,域计算机等)具有许多成员,并且通过“成员”属性以通常方式存储成员资格将导致性能问题。这就是您在memberof属性中看不到“Domain Computers”的原因。

基本上,如果要查找属于“域计算机”成员的计算机,则必须运行查询

(&(objectClass=computer)(primaryGroupID=515))

检查How to use the PrimaryGroupID attribute to find the primary group for a user - 这也适用于计算机。

同时检查Well-known security identifiers in Windows operating systems