gpt4 book ai didi

C# Active Directory 调用非常慢

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

我有一个应用程序需要包含特定安全组中用户的姓名和电子邮件地址列表。我目前正在使用下面的代码执行此操作。当我在 VPN 上运行时,它通常会在一两秒内立即返回,但是当我在以太网或无线(均在域上)上运行时,它需要大约 40 秒才能返回。有什么方法可以改善此方法在以太网或无线网络上的运行时间吗?

        ...
DirectoryEntry entry = new DirectoryEntry(ldap);
DirectorySearcher mySearcher = new DirectorySearcher(entry);
mySearcher.Filter = "(&(objectCategory=group)(objectClass=group)(groupType:1.2.840.113556.1.4.803:=2147483648))";
mySearcher.PropertiesToLoad.Add("member");

SearchResultCollection results = mySearcher.FindAll();
foreach (SearchResult result in results)
{
foreach (string distinguishedMember in result.Properties["member"])
{
string memberPath = "LDAP://" + distinguishedMember;
DirectoryEntry member = new DirectoryEntry(memberPath);
DirectorySearcher Searcher = new DirectorySearcher(member);
Searcher.Filter = "(&(objectCategory=user))";
Searcher.PropertiesToLoad.Add("mail");
Searcher.PropertiesToLoad.Add("name");
SearchResult memberFound = Searcher.FindOne();

if (memberFound != null)
{
String memberEmail = memberFound.Properties["mail"][0].ToString();
String memberName = memberFound.Properties["name"][0].ToString();
users.Add(new KeyValuePair<String, String>(memberName, memberEmail));
}
}
}

最佳答案

也许一次性获取所有用户而不是一个一个地获取它们会有所帮助*:

Searcher.Filter = "(&(objectCategory=user)(memberOf=" + myGroupsDistinguishedName + "))"
Searcher.PropertiesToLoad.Add("mail");
Searcher.PropertiesToLoad.Add("name");
var allMembers = Searcher.FindAll();
var users = allMembers.Cast<SearchResult>().ToDictionary(sr=>sr.Properties["name"].ToString(), sr=>sr.Properties["mail"].ToString());

*这不处理超过 1000 个用户的场景。

关于C# Active Directory 调用非常慢,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25189602/

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