gpt4 book ai didi

c# - 事件目录 : find details of users in group without mass search

转载 作者:行者123 更新时间:2023-11-30 17:53:19 25 4
gpt4 key购买 nike

好的,使用事件目录,我剩下的一个要求是拉回给定组中的所有用户并获取他们的详细信息 - 名字、姓氏、用户名、电子邮件。

阅读完这篇文章后,我惊讶地发现似乎没有一种有效的方式来做到这一点。我想出了两种获得结果的方法,但这两种方法看起来都非常浪费。

首先是搜索群组成员,如下所示:

        DirectoryEntry searchRoot = new DirectoryEntry("LDAP://" + server, username, password);
DirectorySearcher search = new DirectorySearcher(searchRoot);
search.Filter = string.Format("(|(CN={0})(CN={1}))", "Group1", "Group2");

search.PropertiesToLoad.Add("member");
SearchResultCollection results = search.FindAll();

返回包含用户名的单个结果,并且仅包含用户名。您可以将其分解为一组单独的用户名,但随后要获取每个用户的详细信息,您必须在每个名称的基础上再次搜索 AD。

另一种方法是:

   DirectoryEntry searchRoot = new DirectoryEntry("LDAP://" + server, username, password);
DirectorySearcher search = new DirectorySearcher(searchRoot);
search.Filter = string.Format("(&(&(objectClass=user)(objectCategory=person))(memberOf=*))");

search.PropertiesToLoad.Add("memberOf");
search.PropertiesToLoad.Add("name");
search.PropertiesToLoad.Add("mail");
search.PropertiesToLoad.Add("displayName");

SearchResultCollection mySearchResultColl = search.FindAll();

foreach (SearchResult result in mySearchResultColl)
{
foreach (string prop in result.Properties["memberOf"])
{
if (prop.Contains("Group1") || prop.Contain("Group2"))
{
//add user to list
}
}
}

这让我得到了我想要的,但涉及检索每个事件目录用户,然后遍历集合以找到匹配的用户。这在我的小测试目录上运行良好,但一想到它在拥有数千名用户的系统上会有多慢,我就不寒而栗。

我知道您可以使用 PrinicpalContext 对象执行我需要的操作,但据我所知,只有当代码在同一域中运行时它才有效,我不能保证这一点。我需要能够跨域查询。

有更好的方法吗?还是我只能忍受性能问题?

最佳答案

如果只需要查找直接成员,可以使用属性范围查询 (ASQ)。这需要 2003 的域/林功能级别(忘记域或林)。

DirectoryEntry groupEntry = new DirectoryEntry("LDAP://<server>/<group DN>", "user", "pwd");

DirectorySearcher searcher = new DirectorySearcher(groupEntry);
searcher.SearchScope = SearchScope.Base;
searcher.AttributeScopeQuery = "member";
searcher.Filter = "(&(objectCategory=person)(objectClass=user))";
searcher.PropertiesToLoad.Clear();
searcher.PropertiesToLoad.Add("name");
searcher.PropertiesToLoad.Add("mail");
searcher.PropertiesToLoad.Add("displayName");

foreach (SearchResult result in searcher.FindAll())
{
Console.WriteLine(result.Path);
}

对于嵌套组成员,您可以使用 LDAP_MATCHING_RULE_IN_CHAIN 匹配规则。这需要 2008 R2 的域/林功能级别(同样,忘记域或林)。

DirectoryEntry rootEntry = new DirectoryEntry("GC://<server>", "user", "pwd");

DirectorySearcher searcher = new DirectorySearcher(rootEntry);
searcher.SearchScope = SearchScope.Subtree;
searcher.Filter = "(&(objectCategory=person)(objectClass=user)(memberOf:1.2.840.113556.1.4.1941:=<group DN>))";
searcher.PropertiesToLoad.Clear();
searcher.PropertiesToLoad.Add("name");
searcher.PropertiesToLoad.Add("mail");
searcher.PropertiesToLoad.Add("displayName");

foreach (SearchResult result in searcher.FindAll())
{
Console.WriteLine(result.Path);
}

限制:

  • 这两种方法都不处理主要组成员身份
  • ASQ 不能跨域工作,如果发现来自另一个域的成员,将抛出异常。所以通常你只能为全局组执行此操作。

关于c# - 事件目录 : find details of users in group without mass search,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17484852/

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