【发布时间】:2020-03-09 15:29:30
【问题描述】:
我正在尝试构建一种方法来从我的 Active Directory 中获取给定组中的所有成员。
如果我运行以下代码,这似乎很好(我认为),我只会得到一个用户,但不是全部。
这就是我的方法:
static void Main(string[] args)
{
GetNestedGroupName("Groupname");
}
public static void GetNestedGroupName(string groupName)
{
using (PrincipalContext context = new PrincipalContext(ContextType.Domain, "ad.domain.com"))
{
using (GroupPrincipal group = GroupPrincipal.FindByIdentity(context, groupName))
{
if (group != null)
{
foreach (Principal p in group.GetMembers())
{
Console.WriteLine("name: " + p.SamAccountName);
Console.ReadLine();
}
}
else {
Console.WriteLine("nothing");
Console.ReadLine();
}
}
【问题讨论】:
-
build a method wich returns me all Members上面没有返回任何内容。它也会关闭,因为没有什么能阻止控制台保持打开状态。在GetNestedGroupName("Groupname");之后添加Console.ReadLine();。 -
嗨,你说得对,我已经更新了我的代码,但控制台没有保持打开状态。
-
这是因为你抛出了一个异常,所以它关闭了。
-
嗨,我添加了一个新版本,但现在我只有一个用户,而不是全部。
-
那么你能像你提到的那样运行代码但是你得到一个错误吗?如果您在调试模式下单步执行,错误会在哪里引发?我有同样的事情,但没有使用第二个 using 语句。
GroupPrincipal searchGroup= new GroupPrincipal(ctx); searchGroup.Name = "<SearchCriteria*"; // I used a wildcard in my search
标签: c# .net active-directory