首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用PrincipalSearcher查找具有"or“参数的用户

使用PrincipalSearcher查找具有"or“参数的用户
EN

Stack Overflow用户
提问于 2012-05-16 02:02:38
回答 5查看 43.6K关注 0票数 32

是否可以使用System.DirectoryServices.AccountManagement.PrincipalSearcher根据多个参数使用"or“(而不是"and")进行搜索。

代码语言:javascript
复制
// 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();

我想要一个类似的搜索(在LDAP中)使用PrincipalSearcher (而不是DirectorySearcher)

代码语言:javascript
复制
// (&(objectCategory=person)(!UserAccountControl:1.2.840.113556.1.4.803:=2)(|(SAMAccountName=tom*)(DisplayName=tom*)))
EN

回答 5

Stack Overflow用户

发布于 2013-12-18 18:30:43

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

代码语言:javascript
复制
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());
}
票数 24
EN

Stack Overflow用户

发布于 2014-07-23 01:05:02

不一定像其他一些答案那样清晰,但这里是我如何在我正在工作的项目中实现这一点的。我希望这两个搜索都是异步运行的,以尝试减少由于运行两个AD查询而导致的速度减慢。

代码语言:javascript
复制
public async static Task<List<ADUserEntity>> FindUsers(String searchString)
{
    searchString = String.Format("*{0}*", searchString);
    List<ADUserEntity> users = new List<ADUserEntity>();

    using (UserPrincipal searchMaskDisplayname = new UserPrincipal(domainContext) { DisplayName = searchString })
    using (UserPrincipal searchMaskUsername = new UserPrincipal(domainContext) { SamAccountName = searchString })
    using (PrincipalSearcher searcherDisplayname = new PrincipalSearcher(searchMaskDisplayname))
    using (PrincipalSearcher searcherUsername = new PrincipalSearcher(searchMaskUsername))
    using (Task<PrincipalSearchResult<Principal>> taskDisplayname = Task.Run<PrincipalSearchResult<Principal>>(() => searcherDisplayname.FindAll()))
    using (Task<PrincipalSearchResult<Principal>> taskUsername = Task.Run<PrincipalSearchResult<Principal>>(() => searcherUsername.FindAll()))
    {
        foreach (UserPrincipal userPrincipal in (await taskDisplayname).Union(await taskUsername))
            using (userPrincipal)
            {
                users.Add(new ADUserEntity(userPrincipal));
            }
    }

    return users.Distinct().ToList();
}

我的ADUserEntity类有一个基于SID的相等检查。我试图将Distinct()添加到两个搜索器结果的Union()中,但没有成功。

我欢迎对我的回答提出任何建设性的批评,因为我想知道是否有任何方法可以改进它。

票数 8
EN

Stack Overflow用户

发布于 2016-06-09 19:54:22

我知道这有点晚了,但这是我在搜索广告时使用的结构:

代码语言:javascript
复制
public static Task<IEnumerable<SomeUserModelClass>> GetUsers(//Whatever filters you want)
{
    return Task.Run(() =>
    {
        PrincipalContext context = new PrincipalContext(ContextType.Domain);
        UserPrincipal principal = new UserPrincipal(context);
        principal.Enabled = true;
        PrincipalSearcher searcher = new PrincipalSearcher(principal);

        var users = searcher.FindAll().Cast<UserPrincipal>()
            .Where(x => x.SomeProperty... // Perform queries)
            .Select(x => new SomeUserModelClass
            {
                userName = x.SamAccountName,
                email = x.UserPrincipalName,
                guid = x.Guid.Value
            }).OrderBy(x => x.userName).AsEnumerable();

        return users;
    });
}
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/10606334

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档