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

发布于 2024-12-13 19:13:15 字数 1175 浏览 3 评论 0原文

这是我尝试的方法:

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

编辑:

很高兴发现用户属于该组,但我可以获取该组的计算机(memberOF)如果组是“域计算机”或“域控制器”(主要群体!)。

有人可以帮助我吗?

This is what I try:

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

Edit:

It's good to find USERS belong the group but I can get the Computers (memberOF) the group if group is "Domain Computers" or "Domain Controllers" (primary Groups!).

Some one can help me?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

尘世孤行 2024-12-20 19:13:15

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

内置组(域用户、域计算机等)有许多成员,通过“成员”属性以通常的方式存储成员身份会导致性能问题。这就是为什么您在 memberof 属性中看不到“域计算机”的原因。

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

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

检查如何使用 PrimaryGroupID 属性查找用户的主要组 - 这也适用于计算机。

另请检查Windows 操作系统中众所周知的安全标识符

The primary group of an object (user or computer) in AD is stored in a property called "primaryGroupID". For example, for users this is generally 513, which means that the primary group is "Domain Users".

The built-in groups (Domain Users, Domain Computers etc) have many members, and storing the membership in the usual way through the "member" property would cause performance issues. This is why you don't see "Domain Computers" in the memberof property.

Basically, if you want to find computers that are members of "Domain Computers", you must run the query

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

Check How to use the PrimaryGroupID attribute to find the primary group for a user - this applies also to computers.

Also check Well-known security identifiers in Windows operating systems

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文