简体   繁体   English

扩展用户原理极慢

[英]Extended UserPrinciple Extremely Slow

I'm working on a program that needs to loop over 30k AD users. 我正在开发一个程序,该程序需要循环超过30k AD用户。 For the initial incarnation I used System.Management.Automation.PowerShell. 对于最初的化身,我使用了System.Management.Automation.PowerShell。 This works perfect, but I didn't like the taste of executing PowerShell from within a C# all. 这很完美,但是我不喜欢从C#全部内部执行PowerShell的味道。

I then switched to using DirectoryServices.AccountManagement as a more "pure" solution. 然后,我改用DirectoryServices.AccountManagement作为更“纯”的解决方案。 I need to get the user's Title and Manager. 我需要获取用户的标题和管理员。 Due to this I had to extend UserPrinciple. 因此,我不得不扩展UserPrinciple。 However, after doing this I found it takes an extremely long time to get either Title or Manager. 但是,执行此操作后,我发现获得标题或经理需要很长时间。 For my environment it's takes about 160ms to retrieve those 2 properties. 对于我的环境,检索这2个属性大约需要160毫秒。

The delay seems to come when I attempt to access either the Title or Manager properties. 当我尝试访问“标题”或“管理器”属性时,似乎出现了延迟。

Examples: 例子:

Original Code - Each loop iteration takes less than 2ms: 原始代码-每个循环迭代所用的时间少于2ms:

ps.AddCommand("Get-Aduser");
ps.AddParameter("Filter", "*");
ps.AddParameter("SearchBase", dName);
string[] props = { "Name", "EmailAddress", "GivenName", "Surname", "Title", "Manager", "Enabled", "EmployeeID" };
ps.AddParameter("Properties", props);

foreach (PSObject i in ps.Invoke())
{
    Console.WriteLine(i.Properties["Title"].Value.ToString());
}

New Code - Each loop iteration takes about 165ms: 新代码-每个循环迭代大约需要165ms:

PrincipalContext ctx = new PrincipalContext(ContextType.Domain, "*******", distinguishedName);

UserPrincipalsEx qbeUser = new UserPrincipalsEx(ctx);
PrincipalSearcher srch = new PrincipalSearcher(qbeUser);
PrincipalSearchResult<Principal> res = srch.FindAll();
foreach (UserPrincipalsEx i in res)
{
    Console.WriteLine(i.Title);
}

Is there something I am missing here or does it just take extra time to individually query the extended properties? 我在这里缺少什么吗?还是只需要花费一些时间单独查询扩展属性?

The obvious difference for me is that in the Powershell example you are specifying the properties you want before you execute the command. 对我而言,明显的不同是在Powershell示例中,您在执行命令之前指定了所需的属性。

In the C# version you are querying the .title property after having searched AD. 在C#版本中,您在搜索广告后正在查询.title属性。 This probably goes and fetches the record from AD again as that property was not loaded 由于未加载该属性,因此可能会再次从AD获取记录

Is it maybe possible for you to to tell the PrincipalSearcher object what fields you want included in the result? 您是否有可能告诉PrincipalSearcher对象要在结果中包括哪些字段?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM