问题描述
我正在编写第一个代码以读取AD组的所有成员,但没有获得正确数量的组成员:
public static void GetlistofAdUsersByGroup (string domainName,string groupName) {
PrincipalContext ctx = new PrincipalContext (ContextType.Domain); // Set up the domain context
GroupPrincipal group = GroupPrincipal.FindByIdentity (ctx,groupName); // Find the specified group
if (group != null) {
foreach (Principal p in group.GetMembers()) {
Console.WriteLine ("{0}: {1}",p.StructuralObjectClass,p.displayName);
}
}
}
我已经有问题了。 group.Members.Count是3,我将打印这3个成员,但是数量应该大得多。我这么早会做错什么?
编辑:在列出我的3个用户之前,我在“输出”窗口中遇到两个错误。错误是:
引发的异常:mscorlib.dll异常中的“ System.ArgumentException” 抛出:mscorlib.dll中的'System.FormatException'
编辑2:
我基本上是从其他地方偷走了以下代码,并且也从该代码中得到了3个结果!
public static IEnumerable<string> GetGroupMemberList (GroupPrincipal group,bool recursive = false) {
using (var memberPrincipals = group.GetMembers(recursive)) {
foreach (Principal member in memberPrincipals) {
yield return member.SamAccountName;
}
}
}
这样打电话:
PrincipalContext ctx = new PrincipalContext (ContextType.Domain);
GroupPrincipal group = GroupPrincipal.FindByIdentity (ctx,"DOC.WKCC");
_elements = new List<string>(GetGroupMemberList (group,true));
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)