【问题标题】:PrincipalSearcher filter all records that do not matchPrincipalSearcher 过滤所有不匹配的记录
【发布时间】:2015-06-08 23:39:16
【问题描述】:

有没有办法使用 PrincipalSearcher 搜索所有不匹配的记录。使用 DirectorySearcher,您可以应用像 (!sn="\*Jay\*") 这样的过滤器。换句话说,所有姓氏不包含序列“Jay”的记录。我想知道是否有任何方法可以使用 UserPrincipal 参数。

【问题讨论】:

    标签: .net directoryservices


    【解决方案1】:

    很遗憾,这不是一个选项。我花了相当多的时间试图找到一种方法来轻松/有效地进行更高级的搜索。最接近高级搜索的是一些日期选项,但没有文本搜索。

    我最终做的是使用 LDAP 查询单独运行 DirectorySearcher。我从搜索中返回的唯一属性(以最小化结果集大小并提高速度)是 DN 和对象类型(如果尚未过滤对象类型)。然后,我使用 DN 创建一个适当类型的新 Principal 对象并将其添加到集合中。

    整个 AccountManagement 库的设计考虑了非常少的任务(显然),并且很难扩展。对于大多数任务,我几乎已经放弃使用 DirectoryServices,因为它才是真正应该使用的库。

    【讨论】:

      【解决方案2】:

      我似乎总是在回答老问题,但在搜索中不断出现这个问题,我相信我已经弄清楚了。如果它可以帮助找到它的人,这是我设法拼凑的。

      您要做的是创建一个自定义AdvancedFilter 并根据您的需要创建一些自定义搜索规则。默认值相当有限,但您可以创建几乎任何您需要的东西。

      你的问题可以这样处理

      public class MyAdvancedFilter : AdvancedFilters
      {
          public MyAdvancedFilter(Principal principal) : base(principal)
          {
      
          }
      
          public void WhereLastName(string lastName, MatchType match)
          {
              // The * is the normal wildcard for LDAP.
              // Remove *'s for an exact match, or create a parameter to choose what to do.
              this.AdvancedFilterSet("sn", "*" + lastName + "*", typeof(string), match);
          }
      }
      

      为了使用这个,你还必须有一个自定义的UserPrincipal 对象来实现这个。

      [DirectoryObjectClass("user")]
      [DirectoryRdnPrefix("CN")]
      public class MyUser : UserPrincipal
      {
          public MyUser(PrincipalContext context) : base(context)
          {
      
          }
      
          private MyAdvancedFilter searchFilter;
          // use custom search filters
          public new MyAdvancedFilter AdvancedSearchFilter
          {
              get
              {
                  if (searchFilter == null)
                      searchFilter = new MyAdvancedFilter(this);
      
                  return searchFilter;
              }
          }
      }
      

      现在您可以使用此代码了。

      MyUser u = new MyUser();
      // find users without the last name containing "Jay"
      u.AdvancedSearchFilter.WhereLastName("Jay", MatchType.NotEquals);
      
      PrincipalSearcher ps = new PrincipalSearcher(u);
      var res = ps.FindAll().Cast<MyUser>();
      
      foreach (MyUser p in res)
      {
          // use the results here.
      }
      

      希望对某人有所帮助。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-11-30
        • 1970-01-01
        • 2018-04-26
        • 1970-01-01
        • 2019-01-15
        相关资源
        最近更新 更多