似乎 System.DirectoryServices.AccountManagement 提供了一次只能搜索一种类型对象的示例查询。
System.DirectoryServices.AccountManagement 是否证明了一种方法,我可以使用该方法在整个事件目录中搜索与特定名称或某些其他条件匹配的用户或组,否则我必须返回 System .DirectoryServices.DirectorySearcher。
最佳答案
我相信您应该能够在 S.DS.AM 中执行此操作。 UserPrincipal
和 GroupPrincipal
最终都派生自 Principal
- 因此如果您将“通用”委托(delegate)人传递给搜索者,您应该取回用户和组(以及计算机)。
唯一棘手的部分是 Principal
是一个抽象类,因此您不能直接实例化它 - 您需要先获取 UserPrincipal
并“提取”通用 Principal
来自于:
// set up dummy UserPrincipal
UserPrincipal qbeUser = new UserPrincipal(ctx);
// get the generic Principal from that - set the "Name" to search for
Principal userOrGroup = qbeUser as Principal;
userOrGroup.Name = "SomeName";
// create a PrincipalSearcher based on that generic principal
PrincipalSearcher searcher = new PrincipalSearcher(userOrGroup);
// enumerate the results - you need to check what kind of principal you get back
foreach (Principal found in searcher.FindAll())
{
// is it a UserPrincipal - do what you need to do with that...
if (found is UserPrincipal)
{
......
}
else if (found is GroupPrincipal)
{
// if it's a group - do whatever you need to do with a group....
}
}
关于c# - 使用 System.DirectoryServices.AccountManagement 在 c# 中搜索与特定名称匹配的所有组和用户的事件目录?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9074581/