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

PrincipalSearcher 和 DirectorySearcher 的區(qū)別

Difference between PrincipalSearcher and DirectorySearcher(PrincipalSearcher 和 DirectorySearcher 的區(qū)別)
本文介紹了PrincipalSearcher 和 DirectorySearcher 的區(qū)別的處理方法,對(duì)大家解決問(wèn)題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧!

問(wèn)題描述

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

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

推薦答案

我花了很多時(shí)間分析這兩者之間的差異.這是我學(xué)到的.

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

  • DirectorySearcher 來(lái)自 System.DirectoryServices 命名空間.

PrincipalSearcher 來(lái)自 System.DirectoryServices.AccountManagement 命名空間,它建立在 System.DirectoryServices 之上.PrincipalSearcher 在內(nèi)部使用 DirectorySearcher.

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

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

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 更底層,可以處理的不僅僅是用戶、組和計(jì)算機(jī)對(duì)象.

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

對(duì)于一般用途,當(dāng)您使用基本屬性和只有幾個(gè)對(duì)象時(shí),PrincipalSearcher 將導(dǎo)致更少的代碼行和更快的運(yùn)行時(shí)間.

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.

隨著您正在執(zhí)行的任務(wù)變得越高級(jí),優(yōu)勢(shì)似乎就會(huì)消失.例如,如果您期望獲得超過(guò)幾百個(gè)結(jié)果,則必須獲取基礎(chǔ) DirectorySearcher 并設(shè)置 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 中的所有對(duì)象,而 PrincipalSearcher 的限制要大得多.例如,您不能使用 PrincipalSearcher 和類似的類來(lái)修改組織單位.

    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.

    這是我使用 PrincipalSearcher、DirectorySearcher 而不使用 PropertiesToLoadDirectorySearcher 來(lái)分析的圖表使用 PropertiesToLoad.所有測(cè)試...

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

    • 使用 1000
    • PageSize
    • 一共查詢了4278個(gè)用戶對(duì)象
    • 指定以下條件
      • objectClass=user
      • objectCategory=person
      • 不是調(diào)度資源(即 !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;
      

      這篇關(guān)于PrincipalSearcher 和 DirectorySearcher 的區(qū)別的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

      【網(wǎng)站聲明】本站部分內(nèi)容來(lái)源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問(wèn)題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請(qǐng)聯(lián)系我們刪除處理,感謝您的支持!
  • 相關(guān)文檔推薦

    Why shouldn#39;t I always use nullable types in C#(為什么我不應(yīng)該總是在 C# 中使用可空類型)
    C# HasValue vs !=null(C# HasValue vs !=null)
    C# ADO.NET: nulls and DbNull -- is there more efficient syntax?(C# ADO.NET:空值和 DbNull —— 有沒(méi)有更高效的語(yǔ)法?)
    How to set null value to int in c#?(如何在c#中將空值設(shè)置為int?)
    How to handle nulls in LINQ when using Min or Max?(使用 Min 或 Max 時(shí)如何處理 LINQ 中的空值?)
    Method call if not null in C#(在 C# 中如果不為 null 的方法調(diào)用)
    主站蜘蛛池模板: 欧美一级黄 | 国产成人福利 | 免费观看的av毛片的网站 | 久久精品久久久 | 精品一区二区三区在线观看国产 | 中文字幕一区二区三区四区五区 | 狠狠躁天天躁夜夜躁婷婷老牛影视 | 精品亚洲一区二区 | 久久久女女女女999久久 | av片在线观看网站 | 色婷婷婷婷色 | 亚洲一区二区三区视频 | 国产高清亚洲 | 精品一区二区三区在线观看国产 | 欧美手机在线 | 男女免费在线观看视频 | 欧美在线综合 | 91久久精品国产 | av中文字幕在线 | 久草精品在线 | 日韩精品在线免费观看视频 | 中文日本在线 | 精品久久久久久久久久久久久久 | 91视频导航| 91久久久久 | 精品久久一区二区三区 | 日本不卡一区二区三区 | 欧美日韩国产高清视频 | 亚洲精品中文字幕中文字幕 | 日本不卡免费新一二三区 | 欧美日韩一二三区 | 黄色成人在线网站 | 国产精品178页 | 欧美极品在线视频 | 亚洲精品1区2区3区 91免费看片 | 欧美一级片免费看 | 国产91久久精品一区二区 | 欧美日韩在线免费 | 日本在线视频中文字幕 | 欧美在线一区二区三区 | 91激情视频 |