C# 从广告中获取用户详细信息很慢
我使用下面的代码从特定的部门获取一系列关于员工的信息,并从AD返回一个列表 虽然它可以工作,但看起来相当慢,有没有更有效的方法从广告中获取各种用户详细信息C# 从广告中获取用户详细信息很慢,c#,.net,active-directory,ldap,C#,.net,Active Directory,Ldap,我使用下面的代码从特定的部门获取一系列关于员工的信息,并从AD返回一个列表 虽然它可以工作,但看起来相当慢,有没有更有效的方法从广告中获取各种用户详细信息 public static List<Employee> GetEmployeeListForDepartment(string departpment) { using (HostingEnvironment.Impersonate()) {
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();
}
公共静态列表GetEmployeeListForDepartment(字符串部门)
{
使用(HostingEnvironment.Impersonate())
{
PrincipalContext ctx=新PrincipalContext(ContextType.Domain,Domain);
GroupPrincipal gp=GroupPrincipal.FindByIdentity(ctx,部门);
PrincipalSearchResult members=gp.GetMembers();
列出员工=新列表();
foreach(成员中的var成员)
{
var emp=CreateEmployee(成员);
添加(emp);
}
return employees.OrderBy(x=>x.FirstName.ToList();
}
}
私有静态员工CreateEmployee(主要成员)
{
如果(成员!=null)
{
DirectoryEntry de=(作为DirectoryEntry的member.GetUnderlyingObject());
如果(de!=null)
{
字符串mobile=de.Properties.Contains(“mobile”)?de.Properties[“mobile”][0]。ToString():“”;
字符串title=de.Properties.Contains(“description”)?de.Properties[“description”][0]。ToString():“”;
//等等等等。。。
返回新员工{etc..};
}
}
返回新员工();
}
您应该能够直接使用Active Directory API
其中大部分在“System.DirectoryServices”下可用
我手头没有任何代码来做您需要的事情,但我的博客上有一篇大约一年前的文章,展示了如何在AD中创建本地用户帐户,所有这些帐户都使用与获取用户信息所需的程序集相同的程序集
注意:AD本质上是缓慢的,所以如果你有一个大目录,你很可能无法更快地使用它。你的问题是你正在使用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#中需要它,但这很有帮助,而且现在速度更快。棘手的部分是获取组名的FQDN。