查找属于两个 Active Directory 组的用户

Finding users that are members of two active directory groups(查找属于两个 Active Directory 组的用户)
本文介绍了查找属于两个 Active Directory 组的用户的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我需要找到属于两个组(GroupA 和 GroupB)的所有用户.我还需要考虑嵌套组.这样做的最佳方法是什么?

I need to find all users that are members of two groups (GroupA and GroupB). I also need to take into account nested groups. What is the best way to do this?

我知道使用 memberOf 进行 ldap 搜索不会考虑嵌套组.我还可以专门定位这两个组,获取成员列表,并遍历它们,匹配属于两个列表的成员,但组的成员集合也不考虑嵌套组.是否有任何方法适用于嵌套组,或者我是否需要编写自己的递归逻辑?

I know that doing an ldap search using memberOf does not take into account nested groups. I could also locate the two groups specifically, get a list of members, and iterate through them, matching up ones that are members of both lists, but the members collection of a group doesn't take into account nested groups either. Are there any methods that do work with nested groups, or do I need to write my own recursive logic?

编辑嵌套组:如果我有一个名为 GroupA 的安全组.GroupA 可以拥有用户或其他组的成员.如果 GroupB 是 GroupA 的成员,那么它就是我所说的嵌套组".

Edit Nested group: If I have a security group called GroupA. GroupA can have members which are either users or other groups. GroupB is what I am calling a 'nested group' if it is a member of GroupA.

推荐答案

这里有一些在 ActiveDirectory 2003 ans 2008 R2 中工作的东西.我使用 Microsoft LDAP_MATCHING_RULE_IN_CHAIN 来:

Here is something working in an ActiveDirectory 2003 ans 2008 R2. I use Microsoft LDAP_MATCHING_RULE_IN_CHAIN to :

1) 递归搜索(但在一个查询中)第一组中的所有用户(小心它返回来自安全和分发组的用户)

1) Search recursively (but in one query) all the users from the first group (be careful it return users from security and distributions group)

2) 对于第一个查询中的每个用户,我再次递归搜索(但在一个查询中)该用户是否属于第二个组.

2) For each user from the first query, I again search recursively (but in one query) if the user belongs to the second group.

static void Main(string[] args)
{
  //Connection to Active Directory
  string sFromWhere = "LDAP://SRVENTR2:389/dc=societe,dc=fr";
  DirectoryEntry deBase = new DirectoryEntry(sFromWhere, "societe\administrateur", "test.2011");

  // To find all the users member of groups "Grp1"  :
  // Set the base to the groups container DN; for example root DN (dc=societe,dc=fr) 
  // Set the scope to subtree
  // Use the following filter :
  // (member:1.2.840.113556.1.4.1941:=CN=Grp1,OU=MonOu,DC=X)
  //
  DirectorySearcher dsLookFor = new DirectorySearcher(deBase);
  dsLookFor.Filter = "(&(memberof:1.2.840.113556.1.4.1941:=CN=Grp1,OU=MonOu,DC=societe,DC=fr)(objectCategory=user))";
  dsLookFor.SearchScope = SearchScope.Subtree;
  dsLookFor.PropertiesToLoad.Add("cn");

  SearchResultCollection srcUsers = dsLookFor.FindAll();

  // Just to know if user is present in an other group
  foreach (SearchResult srcUser in srcUsers)
  {
    Console.WriteLine("{0}", srcUser.Path);

    // To check if a user "user1" is a member of group "group1".
    // Set the base to the user DN (cn=user1, cn=users, dc=x)
    // Set the scope to base
    // Use the following filter :
    // (memberof:1.2.840.113556.1.4.1941:=(cn=Group1,OU=groupsOU,DC=x))
    DirectoryEntry deBaseUsr = new DirectoryEntry(srcUser.Path, "societe\administrateur", "test.2011");
    DirectorySearcher dsVerify = new DirectorySearcher(deBaseUsr);
    dsVerify.Filter = "(memberof:1.2.840.113556.1.4.1941:=CN=Grp3,OU=MonOu,DC=societe,DC=fr)";
    dsVerify.SearchScope = SearchScope.Base;
    dsVerify.PropertiesToLoad.Add("cn");

    SearchResult srcTheUser = dsVerify.FindOne();

    if (srcTheUser != null)
    {
      Console.WriteLine("Bingo {0}", srcTheUser.Path);
    }
  }
  Console.ReadLine();
}

这篇关于查找属于两个 Active Directory 组的用户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

How to determine the type (AD User vs. AD Group) of an account?(如何确定帐户的类型(AD 用户与 AD 组)?)
How to resolve quot;The server does not support the control. The control is critical.quot; Active Directory error(如何解决“服务器不支持控件.控制至关重要.活动目录错误)
How to authenticate users with a customer#39;s (remote) active directory server(如何使用客户的(远程)活动目录服务器对用户进行身份验证)
How to know if my DirectoryEntry is really connected to my LDAP directory?(如何知道我的 DirectoryEntry 是否真的连接到我的 LDAP 目录?)
Add member to AD group from a trusted domain(将成员从受信任的域添加到 AD 组)
How to retrieve Users in a Group, including primary group users(如何检索组中的用户,包括主要组用户)