問(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
而不使用 PropertiesToLoad
和 DirectorySearcher
來(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
的 - 一共查詢了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); }
使用
DirectorySearcher
和PropertiesToLoad
Using
DirectorySearcher
withPropertiesToLoad
與使用
DirectorySearcher
"相同,但添加這一行Same as "Using
DirectorySearcher
but add this linesearch.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)系我們刪除處理,感謝您的支持!
PageSize