gpt4 book ai didi

c# - 查找属于两个事件目录组的用户

转载 作者:太空宇宙 更新时间:2023-11-03 11:35:21 26 4
gpt4 key购买 nike

我需要找到属于两个组(GroupA 和 GroupB)的所有用户。我还需要考虑嵌套组。执行此操作的最佳方法是什么?

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

编辑嵌套组:如果我有一个名为 GroupA 的安全组。 GroupA 的成员可以是用户或其他组。如果 GroupB 是 GroupA 的成员,我称之为“嵌套组”。

最佳答案

这是在 ActiveDirectory 2003 和 2008 R2 中工作的东西。我用 Microsoft LDAP_MATCHING_RULE_IN_CHAIN到:

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

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

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

关于c# - 查找属于两个事件目录组的用户,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6457715/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com