問題描述
我看到了使用 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.
這是我使用 PrincipalSearcher
、DirectorySearcher
而不使用 PropertiesToLoad
和 DirectorySearcher
來分析的圖表使用 PropertiesToLoad
.所有測試...
Here is a chart I made to analyze using PrincipalSearcher
, DirectorySearcher
without using PropertiesToLoad
, and DirectorySearcher
with using PropertiesToLoad
. All tests...
- 使用
1000
的 - 一共查詢了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); }
使用
DirectorySearcher
和PropertiesToLoad
Using
DirectorySearcher
withPropertiesToLoad
與使用
DirectorySearcher
"相同,但添加這一行Same as "Using
DirectorySearcher
but add this linesearch.PropertiesToLoad.AddRange(new string[] { "canonicalName" });
之后
search.PageSize = 1000;
這篇關于PrincipalSearcher 和 DirectorySearcher 的區別的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!
【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!
PageSize