在Web应用程序中,我们正在寻找显示特定组成员的用户的sam帐户列表。在许多情况下,组可能拥有500个或更多成员,我们需要该页面具有响应能力。使用C#获取Active Directory中组成员列表的快速方法
约500名成员组成的团队需要7-8秒才能获得该组所有成员的sam帐户列表。有更快的方法吗?我知道Active Directory管理控制台不到一秒钟就完成了。
我已经尝试了一些方法:
1)
PrincipalContext pcRoot = new PrincipalContext(ContextType.Domain)
GroupPrincipal grp = GroupPrincipal.FindByIdentity(pcRoot, "MyGroup");
List<string> lst = grp.Members.Select(g => g.SamAccountName).ToList();
2)
PrincipalContext pcRoot = new PrincipalContext(ContextType.Domain)
GroupPrincipal grp = GroupPrincipal.FindByIdentity(pcRoot, "MyGroup");
PrincipalSearchResult<Principal> lstMembers = grp.GetMembers(true);
List<string> lst = new List<string>();
foreach (Principal member in lstMembers)
{
if (member.StructuralObjectClass.Equals("user"))
{
lst.Add(member .SamAccountName);
}
}
3)
PrincipalContext pcRoot = new PrincipalContext(ContextType.Domain)
GroupPrincipal grp = GroupPrincipal.FindByIdentity(pcRoot, "MyGroup");
System.DirectoryServices.DirectoryEntry de = (System.DirectoryServices.DirectoryEntry)grp.GetUnderlyingObject();
List<string> lst = new List<string>();
foreach (string sDN in de.Properties["member"])
{
System.DirectoryServices.DirectoryEntry deMember = new System.DirectoryServices.DirectoryEntry("LDAP://" + sDN);
lst.Add(deMember.Properties["samAccountName"].Value.ToString());
}
这与我的问题中的例3类似,需要记录时间的部分是通过成员属性中的每个项目进行交互。 – Jeremy
必须爱匈牙利符号,从一个原始示例 –