c# - 使用 PrincipalSearcher 查找参数为 "or"的用户

标签 c# .net active-directory

是否可以使用 System.DirectoryServices.AccountManagement.PrincipalSearcher 使用“或”(而不是“和”)基于多个参数进行搜索。

// This uses an and
//(&(objectCategory=person)(!UserAccountControl:1.2.840.113556.1.4.803:=2)(&(SAMAccountName=tom*)(DisplayName=tom*)))
var searchPrinciple = new UserPrincipal(context);
searchPrinciple.DisplayName =  "tom*";
searchPrinciple.SamAccountName = "tom*";

var searcher = new PrincipalSearcher();
searcher.QueryFilter = searchPrinciple;

var results = searcher.FindAll();

我想使用 PrincipalSearcher(不是 DirectorySearcher)进行类似的搜索(在 LDAP 中)

// (&(objectCategory=person)(!UserAccountControl:1.2.840.113556.1.4.803:=2)(|(SAMAccountName=tom*)(DisplayName=tom*)))

最佳答案

这显然是不可能的,这里有一个解决方法:

List<UserPrincipal> searchPrinciples = new List<UserPrincipal>();
searchPrinciples.Add(new UserPrincipal(context) { DisplayName="tom*"});
searchPrinciples.Add(new UserPrincipal(context) { SamAccountName = "tom*" });
searchPrinciples.Add(new UserPrincipal(context) { MiddleName = "tom*" });
searchPrinciples.Add(new UserPrincipal(context) { GivenName = "tom*" });

List<Principal> results = new List<Principal>();
var searcher = new PrincipalSearcher();
foreach (var item in searchPrinciples)
{
    searcher = new PrincipalSearcher(item);
    results.AddRange(searcher.FindAll());
}

关于c# - 使用 PrincipalSearcher 查找参数为 "or"的用户,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10606334/

相关文章:

active-directory - 我可以模拟通过表单例份验证的客户端并建立与 SQL Server 的受信任连接吗?

javascript - 网络浏览器未在 Windows Phone 8 上加载页面内容

c# - LINQ 查询添加 orderby 使 Skip 和 Take 不起作用 Linqpad

c# - 如何在不破坏MVVM约定的情况下在第三方控件上调用方法?

.net - WPF 进度条无法正确左对齐

c# - 使用 .NET 将文件发布到服务器 HttpWebRequest 或 WebClient

java - 使用 ActiveDirectoryLdapAuthenticationProvider 进行集成测试

c# - 二进制到十进制的转换 - 公式?

.net - 何时使用 'nested diagnostic context' (NDC)?

azure - 将 Azure Active Directory 应用程序访问限制为特定租户