c# - 使用 System.DirectoryServices.AccountManagement 在 c# 中搜索与特定名称匹配的所有组和用户的事件目录?

标签 c# active-directory

似乎 System.DirectoryServices.AccountManagement 提供了一次只能搜索一种类型对象的示例查询。

System.DirectoryServices.AccountManagement 是否证明了一种方法,我可以使用该方法在整个事件目录中搜索与特定名称或某些其他条件匹配的用户或组,否则我必须返回 System .DirectoryServices.DirectorySearcher

最佳答案

我相信您应该能够在 S.DS.AM 中执行此操作。 UserPrincipalGroupPrincipal 最终都派生自 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/

相关文章:

azure - 如何在 Powershell 中向 Azure AAD 应用程序授予管理员同意?

c# - 使用 Active Directory 获取角色列表

c# - 从 linq to sql 中的子查询中选择前 1 个结果

c# - 如何以编程方式启用 Net.Tcp 端口共享服务?

active-directory - ADAM、事件目录、LDAP、ADFS、身份

调用 GetObject ("WinNT://JohnDoe,User"时出现 VBScript 错误)

powershell - Get-ADGroupMember : The size limit for this request was exceeded

c# - 您如何在 WCF ServiceAuthorizationManager 中模拟 IContextChannel?

c# - 如何追踪我从哪里泄漏了 IDisposable 对象?

c# - 如何从 C# 中的目录中获取文件列表