在PrincipalSearcher Query C#中防止子容器对象

时间:2011-09-02 13:49:58

标签: c# directoryservices userprincipal

如何使用子容器(子OU)阻止查询特定OU的子容器对象?

为了澄清,我不想在结果集中的子OU(子容器)中包含用户对象。

例如another stackoverflow post上的代码:

// create a principal object representation to describe
// what will be searched 
UserPrincipal user = new UserPrincipal(adPrincipalContext);

// define the properties of the search (this can use wildcards)
user.Enabled = false;
user.Name = "user*";

// create a principal searcher for running a search operation
PrincipalSearcher pS = new PrincipalSearcher();

// assign the query filter property for the principal object 
// you created
// you can also pass the user principal in the 
// PrincipalSearcher constructor
pS.QueryFilter = user;

// run the query
PrincipalSearchResult<Principal> results = pS.FindAll();

Console.WriteLine("Disabled accounts starting with a name of 'user':");
foreach (Principal result in results)
{
    Console.WriteLine("name: {0}", result.Name);
}

谢谢,

维克多

1 个答案:

答案 0 :(得分:3)

不幸的是,这个(以及其他一些)功能不能直接在PrincipalSearcher类上看到。

您需要“触及”基础DirectorySearcher以设置此类选项(例如页面大小):

DirectorySearcher ds = pS.GetUnderlyingSearcher() as DirectorySearcher;

if(ds != null)
{
   ds.SearchScope = SearchScope.Base;  // or SearchScope.OneLevel - your pick
}