在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
必須愛匈牙利符號,從一個原始示例 –