gpt4 book ai didi

c# - 从 AD 获取用户详细信息很慢

转载 作者:太空狗 更新时间:2023-10-29 18:27:51 26 4
gpt4 key购买 nike

我使用以下代码从特定部门获取大量有关员工的信息并从 AD 返回列表...

虽然它有效,但似乎很慢,是否有更有效的方法从 AD 获取各种用户详细信息?

public static List<Employee> GetEmployeeListForDepartment(string departpment)
{
using (HostingEnvironment.Impersonate())
{

PrincipalContext ctx = new PrincipalContext(ContextType.Domain, domain);
GroupPrincipal gp = GroupPrincipal.FindByIdentity(ctx, departpment);
PrincipalSearchResult<Principal> members = gp.GetMembers();
List<Employee> employees = new List<Employee>();
foreach (var member in members)
{
var emp = CreateEmployee(member);
employees.Add(emp);
}
return employees.OrderBy(x => x.FirstName).ToList();
}
}

private static Employee CreateEmployee(Principal member)
{
if (member != null)
{
DirectoryEntry de = (member.GetUnderlyingObject() as DirectoryEntry);
if (de != null)
{

string mobile = de.Properties.Contains("mobile") ? de.Properties["mobile"][0].ToString() : "";
string title = de.Properties.Contains("description") ? de.Properties["description"][0].ToString() : "";

//ETC ETC...
return new Employee { etc.. };

}

}
return new Employee();
}

最佳答案

您的问题是您正在使用 System.DirectoryServices.AccountManagement...虽然我讨厌这么说,但遗憾的是这是事实。 AccountManagement 在幕后的工作方式是它运行一个单独的 LDAP 查询来单独检索每个项目。因此,当您遍历成员时,它会通过 LDAP 为每个成员进行单独的回调。您要做的是使用 System.DirectoryServices.DirectorySearcher 运行 LDAP 查询。

我的假设是部门是一个组,这取决于您如何使用它。这是我会怎么做。 (我的代码在 VB.Net 中……抱歉)。确保为您的组获取完全限定的 DN,或者提前查找并将其插入查询。

Dim results = LDAPQuery("(memberOf=CN=Fully,OU=Qualified,DC=Group,DC=Distinguished,DC=Name)", New String() {"mobile", "description"})

Dim emps = (from c as System.DirectoryServices.SearchResult in results _
Select New Employee() {.Name = c.Properties("description"), .Mobile = c.Properties("mobile")}).ToList()

Public Function LDAPQuery(ByVal query As String, ByVal attributes As String()) As SearchResultCollection
'create directory searcher from CurrentADContext (no special security available)
Dim ds As New DirectorySearcher(System.DirectoryServices.ActiveDirectory.Domain.GetCurrentDomain().GetDirectoryEntry())
ds.PageSize = 1000

'set filter string
ds.Filter = query

'specify properties to retrieve
ds.PropertiesToLoad.AddRange(attributes)

'get results
Dim results As SearchResultCollection = ds.FindAll()

Return results
End Function

关于c# - 从 AD 获取用户详细信息很慢,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7955514/

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