gpt4 book ai didi

c# - AD PrincipalSearcher : Search where property does not contain some value

转载 作者:行者123 更新时间:2023-11-30 16:09:27 28 4
gpt4 key购买 nike

在构建过滤器以查找具有特定值的对象时,Principal Searcher 似乎做得很好。没有呢?例如,我如何构建一个过滤器来排除名字中带有“Joe”的所有人。下面的代码将不起作用。

        PrincipalContext ctx = new PrincipalContext(ContextType.Domain);
UserPrincipal qbeUser = new UserPrincipal(ctx);
PrincipalSearcher srch = new PrincipalSearcher(qbeUser);

//this is the problem line. How to format to exclude values with Joe?
qbeUser.Name != "*Joe*";

srch.QueryFilter = qbeUser;
foreach (var found in srch.FindAll())
{ do something to non Joe users... }

....

最佳答案

似乎 PrincipalSearcher 是不可能的。

两种可能的解决方法:

  1. 使用PrincipalSearcher获取所有用户并在客户端进行过滤

    PrincipalContext ctx = new PrincipalContext(ContextType.Domain);
    UserPrincipal qbeUser = new UserPrincipal(ctx);
    PrincipalSearcher srch = new PrincipalSearcher(qbeUser);

    srch.QueryFilter = qbeUser;
    foreach (var found in srch.FindAll())
    { //filter out users with "Joe" in its name }
  2. 使用目录搜索器

    DirectoryEntry de = new DirectoryEntry("LDAP://domain.com/dc=domain,dc=com", "user", "pwd");
    DirectorySearcher srch = new DirectorySearcher(de);

    srch.Filter = "(&(objectCategory=person)(objectClass=user)(!(name=*Joe*)))";
    srch.SearchScope = SearchScope.Subtree;
    // add the attributes
    srch.PropertiesToLoad.Add("distinguishedName");
    using (SearchResultCollection results = srch.FindAll())
    {
    foreach (SearchResult result in results)
    {
    string dn = result.Properties["distinguishedName"][0] as string;
    Console.WriteLine("- {0}", dn);
    }
    }

关于c# - AD PrincipalSearcher : Search where property does not contain some value,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27624113/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com