且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

使用PrincipalSearcher找到与"用户;或QUOT;参数

更新时间:2022-10-23 16:55:29

这abviously不可能的,这里是一个workarround:

 列表< UserPrincipal> searchPrinciples =新的List< UserPrincipal>(); 
searchPrinciples.Add(新UserPrincipal(上下文){显示名称=汤姆*});
searchPrinciples.Add(新UserPrincipal(上下文){SAM帐户=汤姆*});
searchPrinciples.Add(新UserPrincipal(上下文){中间名=汤姆*});
searchPrinciples.Add(新UserPrincipal(上下文){给定名称=汤姆*});

名单,LT;主>结果=新的List<主>();
变种搜索=新PrincipalSearcher();
的foreach(在searchPrinciples VAR项)
{
=搜索新PrincipalSearcher(项目);
results.AddRange(searcher.FindAll());
}


Is it possible to use System.DirectoryServices.AccountManagement.PrincipalSearcher to search based on multiple parameters using "or" (not "and").

i.e.

// 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();

and I would like a search similar to this (in LDAP) using PrincipalSearcher (not DirectorySearcher)

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

It's abviously not possible, here is a workarround:

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());
}