创建一个用于访问本地PrincipalSearcher的ActiveDirectory实例大约需要11-22秒。有趣的是,时间总是11或22秒,+/-几毫秒。我试图读出的OU由不超过30个用户和20个组组成。如果我理解正确的话,那么在创建PrincipalSearcher的过程中就会完全阅读AD。但是因为我的广告很小,所以我不希望花那么长时间。我是做错什么了还是这是一种预期的行为?
这是我用来再现所描述的行为的测试代码。
const string name = "localhost:389";
const string container = "OU=OUNAME,DC=DCNAME,DC=local";
var principalContext = new PrincipalContext(ContextType.ApplicationDirectory, name, container);
var userPrincipal = new UserPrincipal(principalContext);
// The next call takes about 22 - 44 seconds
var principalSearcher = new PrincipalSearcher(userPrincipal);
var result = principalSearcher.FindAll();当我进行性能分析时,我会得到以下结果:

更新#1:
本地AD是用and完成的,我发现this question似乎描述了类似的问题。但我同时拥有AD和应用程序,它们运行在同一台机器上,我使用的是localhost或127.0.0.1。所以这对我的案子没什么帮助。
更新2:
如果我不再连接到公司的域名,相同的代码只需100‘s。是什么导致了这种不同的行为?更重要的是,在连接到域时,我如何克服这种行为?
发布于 2022-05-10 14:30:23
尝试使用options参数的PrincipalContext constructor,看看某些组合是否解决了您的问题。
默认的(如果您没有指定任何内容)是:
ContextOptions.Negotiate | ContextOptions.Signing | ContextOptions.Sealing但是看看combination of ContextOptions有什么不同,如果有的话。
https://stackoverflow.com/questions/72183758
复制相似问题