久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

PrincipalSearcher 和 DirectorySearcher 的區別

Difference between PrincipalSearcher and DirectorySearcher(PrincipalSearcher 和 DirectorySearcher 的區別)
本文介紹了PrincipalSearcher 和 DirectorySearcher 的區別的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我看到了使用 PrincipalSearcher 的 Active Directory 示例,以及使用 DirectorySearcher 執行相同操作的其他示例.這兩個例子有什么區別?

I see Active Directory examples that use PrincipalSearcher and other examples that do the same thing but use DirectorySearcher. What is the difference between these two examples?

使用 PrincipalSearcher

PrincipalContext context = new PrincipalContext(ContextType.Domain);
PrincipalSearcher search = new PrincipalSearcher(new UserPrincipal(context));
foreach( UserPrincipal user in search.FindAll() )
{
    if( null != user )
        Console.WriteLine(user.DistinguishedName);
}

使用DirectorySearcher

DirectorySearcher search = new DirectorySearcher("(&(objectClass=user)(objectCategory=person))");
search.PageSize = 1000;
foreach( SearchResult result in search.FindAll() )
{
    DirectoryEntry user = result.GetDirectoryEntry();
    if( null != user )
        Console.WriteLine(user.Properties["distinguishedName"].Value.ToString());
}

推薦答案

我花了很多時間分析這兩者之間的差異.這是我學到的.

I've spent a lot of time analyzing the differences between these two. Here's what I've learned.

  • DirectorySearcher 來自 System.DirectoryServices 命名空間.

PrincipalSearcher 來自 System.DirectoryServices.AccountManagement 命名空間,它建立在 System.DirectoryServices 之上.PrincipalSearcher 在內部使用 DirectorySearcher.

PrincipalSearcher comes from the System.DirectoryServices.AccountManagement namespace, which is built on top of System.DirectoryServices. PrincipalSearcher internally uses DirectorySearcher.

AccountManagement 命名空間(即 PrincipalSearcher)旨在簡化用戶、組和計算機對象(即主體)的管理.理論上,它的用法應該更容易理解,并且產生更少的代碼行.盡管到目前為止,在我的實踐中,這似乎在很大程度上取決于您在做什么.

The AccountManagement namespace (i.e. PrincipalSearcher) was designed to simplify management of User, Group, and Computer objects (i.e. Principals). In theory, it's usage should be easier to understand, and produce fewer lines of code. Though in my practice so far, it seems to heavily depend on what you're doing.

DirectorySearcher 更底層,可以處理的不僅僅是用戶、組和計算機對象.

DirectorySearcher is more low-level and can deal with more than just User, Group and Computer objects.

對于一般用途,當您使用基本屬性和只有幾個對象時,PrincipalSearcher 將導致更少的代碼行和更快的運行時間.

For general usage, when you're working with basic attributes and only a few objects, PrincipalSearcher will result in fewer lines of code and faster run time.

隨著您正在執行的任務變得越高級,優勢似乎就會消失.例如,如果您期望獲得超過幾百個結果,則必須獲取基礎 DirectorySearcher 并設置 PageSize

The advantage seems to disappear the more advanced the tasks you're doing become. For instance if you're expecting more than few hundred results, you'll have to get the underlying DirectorySearcher and set the PageSize

DirectorySearcher ds = search.GetUnderlyingSearcher() as DirectorySearcher;
if( ds != null )
    ds.PageSize = 1000;

如果您使用 PropertiesToLoad

  • DirectorySearcher 可以比 PrincipalSearcher 快得多.

  • DirectorySearcher can be significantly faster than PrincipalSearcher if you make use of PropertiesToLoad.

    DirectorySearcher 和類似的類可以處理 AD 中的所有對象,而 PrincipalSearcher 的限制要大得多.例如,您不能使用 PrincipalSearcher 和類似的類來修改組織單位.

    DirectorySearcher and like classes can work with all objects in AD, whereas PrincipalSearcher is much more limited. For example, you can not modify an Organizational Unit using PrincipalSearcher and like classes.

    這是我使用 PrincipalSearcherDirectorySearcher 而不使用 PropertiesToLoadDirectorySearcher 來分析的圖表使用 PropertiesToLoad.所有測試...

    Here is a chart I made to analyze using PrincipalSearcher, DirectorySearcher without using PropertiesToLoad, and DirectorySearcher with using PropertiesToLoad. All tests...

    • 使用 1000
    • PageSize
    • 一共查詢了4278個用戶對象
    • 指定以下條件
      • objectClass=user
      • objectCategory=person
      • 不是調度資源(即 !msExchResourceMetaData=ResourceType:Room)
      • 已啟用(即 !userAccountControl:1.2.840.113556.1.4.803:=2)


      使用PrincipalSearcher

      [DirectoryRdnPrefix("CN")]
      [DirectoryObjectClass("Person")]
      public class UserPrincipalEx: UserPrincipal
      {
      
          private AdvancedFiltersEx _advancedFilters;
      
          public UserPrincipalEx( PrincipalContext context ): base(context)
          {
              this.ExtensionSet("objectCategory","User");
          }
      
          public new AdvancedFiltersEx AdvancedSearchFilter
          {
              get {
                  if( null == _advancedFilters )
                      _advancedFilters = new AdvancedFiltersEx(this);
                      return _advancedFilters;
              }
          }
      
      }
      
      public class AdvancedFiltersEx: AdvancedFilters 
      {
      
          public AdvancedFiltersEx( Principal principal ): 
              base(principal) { }
      
          public void Person()
          {
              this.AdvancedFilterSet("objectCategory", "person", typeof(string), MatchType.Equals);
              this.AdvancedFilterSet("msExchResourceMetaData", "ResourceType:Room", typeof(string), MatchType.NotEquals);
          }
      }
      
      //...
      
      for( int i = 0; i < 10; i++ )
      {
          uint count = 0;
          Stopwatch timer = Stopwatch.StartNew();
          PrincipalContext context = new PrincipalContext(ContextType.Domain);
          UserPrincipalEx filter = new UserPrincipalEx(context);
          filter.Enabled = true;
          filter.AdvancedSearchFilter.Person();
          PrincipalSearcher search = new PrincipalSearcher(filter);
          DirectorySearcher ds = search.GetUnderlyingSearcher() as DirectorySearcher;
          if( ds != null )
              ds.PageSize = 1000;
          foreach( UserPrincipalEx result in search.FindAll() )
          {
              string canonicalName = result.CanonicalName;
              count++;
          }
      
          timer.Stop();
          Console.WriteLine("{0}, {1} ms", count, timer.ElapsedMilliseconds);
      }
      


      使用DirectorySearcher

      for( int i = 0; i < 10; i++ )
      {
          uint count = 0;
          string queryString = "(&(objectClass=user)(objectCategory=person)(!msExchResourceMetaData=ResourceType:Room)(!userAccountControl:1.2.840.113556.1.4.803:=2))";
      
          Stopwatch timer = Stopwatch.StartNew();
      
          DirectoryEntry entry = new DirectoryEntry();
          DirectorySearcher search = new DirectorySearcher(entry,queryString);
          search.PageSize = 1000;
          foreach( SearchResult result in search.FindAll() )
          {
              DirectoryEntry user = result.GetDirectoryEntry();
              if( user != null )
              {
                  user.RefreshCache(new string[]{"canonicalName"});
                  string canonicalName = user.Properties["canonicalName"].Value.ToString();
                  count++;
              }
          }
          timer.Stop();
          Console.WriteLine("{0}, {1} ms", count, timer.ElapsedMilliseconds);
      }
      


      使用 DirectorySearcherPropertiesToLoad

      Using DirectorySearcher with PropertiesToLoad

      與使用DirectorySearcher"相同,但添加這一行

      Same as "Using DirectorySearcher but add this line

      search.PropertiesToLoad.AddRange(new string[] { "canonicalName" });
      

      之后

      search.PageSize = 1000;
      

      這篇關于PrincipalSearcher 和 DirectorySearcher 的區別的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

      【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!
  • 相關文檔推薦

    Why shouldn#39;t I always use nullable types in C#(為什么我不應該總是在 C# 中使用可空類型)
    C# HasValue vs !=null(C# HasValue vs !=null)
    C# ADO.NET: nulls and DbNull -- is there more efficient syntax?(C# ADO.NET:空值和 DbNull —— 有沒有更高效的語法?)
    How to set null value to int in c#?(如何在c#中將空值設置為int?)
    How to handle nulls in LINQ when using Min or Max?(使用 Min 或 Max 時如何處理 LINQ 中的空值?)
    Method call if not null in C#(在 C# 中如果不為 null 的方法調用)
    主站蜘蛛池模板: 在线成人免费视频 | 亚洲电影免费 | 国产高清在线精品一区二区三区 | 韩国理论电影在线 | 99riav3国产精品视频 | 精品成人| 欧美日韩国产精品一区 | 精品国产乱码久久久久久蜜柚 | 99热都是精品 | 午夜免费 | a网站在线观看 | 91视频国产一区 | av看片| 成人av观看 | 成人在线视频免费观看 | 国产日韩欧美一区二区 | 国产一区二区在线播放 | 国产一区二区三区在线看 | 激情一区二区三区 | 国产有码| 亚洲在线一区 | 精品九九 | 午夜免费观看体验区 | 国产视频线观看永久免费 | a看片| 一本大道久久a久久精二百 欧洲一区二区三区 | 国产不卡一| 国产成人精品免费 | 久久国产婷婷国产香蕉 | 亚洲精品一区二区三区在线 | 91综合网 | 亚洲精品久 | 夏同学福利网 | 久久视频精品 | 日韩精品免费在线 | 成人免费视频网站在线观看 | 欧美日产国产成人免费图片 | 精品久久久久久亚洲精品 | 一区二区三区av夏目彩春 | 亚洲福利av| 欧美日产国产成人免费图片 |