我在使用这段代码时遇到了一些困难,尤其是在使用 PrincipalSearcher 时。我正在尝试获取与特定 OU 关联的所有组的列表。
我试图只返回所有组范围下的“安全”组,不包括通讯组。
我遇到的问题是,除了我打算返回的那些之外,它还返回了这些内置组。
帮助服务组 Telnet客户端 管理员 用户 嘉宾 打印运算符(operator) 备份运算符(operator) 复制器 远程桌面用户 网络配置运算符(operator) 性能监视器用户 性能日志用户 分布式 COM 用户 域计算机 域 Controller 架构管理员 企业管理员 证书发行商 域管理员 域用户 域 guest 组策略创建者所有者 RAS 和 IAS 服务器 服务器运营商 帐户运算符(operator) Windows 2000 之前的兼容访问 即将到来的森林信托 build 者 Windows 授权访问组 终端服务器许可证服务器 Dns管理员 Dns更新代理 IIS_WPG
我不确定范围是否不正确或者我是否缺少某种过滤。
相关代码段:
public static ArrayList GetAllGroups()
{
var myItems = new ArrayList();
var ctx = new PrincipalContext(ContextType.Domain,"MyOU");
// define a "query-by-example" principal - here, we search for a GroupPrincipal
var qbeGroup = new GroupPrincipal(ctx);
// create your principal searcher passing in the QBE principal
var srch = new PrincipalSearcher(qbeGroup);
// find all matches
foreach (Principal found in srch.FindAll())
{
var foundGroup = found as GroupPrincipal;
if (foundGroup != null)
{
myItems.Add(foundGroup.Name);
}
}
return myItems;
}
我如何才能排除内置组?
如有任何帮助,我们将不胜感激。
谢谢!
最佳答案
两件事:
没有组关联一个 OU - OU 是一个容器,包含用户、计算机、组等。 (就像包含文件的目录)。你是这个意思吗?您想要枚举给定 OU 中包含的组??
如果是这样:您没有正确调用
PrincipalContext
的构造函数。如果您检查 MSDN documentation onPrincipalContext
constructors ,您会看到您正在使用的是带有ContextType
和name
的那个,它代表 domain name您要绑定(bind)到的上下文:var ctx = new PrincipalContext(ContextType.Domain,"MyOU");
这绑定(bind)到
MyOU
域 - 并且它绑定(bind)在该域树的根部。
您可能正在寻找具有三个参数的构造函数 - 一个 ContextType
和 两个字符串 - 第一个是上面的域名,第二个一个是您搜索的起始位置。因此,将您的 PrincipalContext
构造更改为:
var ctx = new PrincipalContext(ContextType.Domain, "YOURDOMAIN", "OU=MyOU");
然后再次搜索 - 现在您应该只会得到包含在 OU=MyOU
容器中的组。
关于C# PrincipalSearcher,返回特定 OU 的 AD 组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9981410/