首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用PrincipalSearcher进行单级搜索

使用PrincipalSearcher进行单级搜索
EN

Stack Overflow用户
提问于 2013-07-12 02:03:53
回答 2查看 2.5K关注 0票数 1

我最近刚开始学习C#,由于工作上的需要,我正在尝试编写一个方法,该方法将查询Active Directory中的特定OU,并且只查询该OU,而不查询子OU。该方法如下所示:

代码语言:javascript
复制
public List<string> getAllActiveUsers()
{
    PrincipalContext oPrincipalContext = GetPrincipalContext();
    UserPrincipal oUserPrincipal = new UserPrincipal(oPrincipalContext) 
    { 
        Enabled = true 
    };
    PrincipalSearcher oPrincipalSearcher = new PrincipalSearcher(oUserPrincipal);

    List<string> allUsers = new List<string>();

    foreach (var found in oPrincipalSearcher.FindAll())
    {
        allUsers.Add(found.DisplayName.ToString());
    }
    allUsers.Sort();
    return allUsers;
}

现在的方法只拉取启用了用户的用户帐户,但问题是它拉取subOUs中的帐户,这是不可取的。我已经用谷歌搜索了相当长一段时间,对于这个问题没有真正的答案,我充其量只是一个新手,如果有修改代码的建议,请告诉我最终的方法是什么样子的。

任何和所有的帮助都是感激的。

谢谢!

更新:显然我的谷歌搜索还不够,所以我采取了另一种方法,我遇到了一个建议,使用GetUnderlyingSearcher()可以工作,但我仍然不知道如何使用它。一些额外的研究得出了我需要的结果,以下是更新后的方法:

代码语言:javascript
复制
public List<string> getAllActiveUsers()
    {
        PrincipalContext oPrincipalContext = GetPrincipalContext();
        UserPrincipal oUserPrincipal = new UserPrincipal(oPrincipalContext) { Enabled = true };
        PrincipalSearcher oPrincipalSearcher = new PrincipalSearcher(oUserPrincipal);
        //Setting the search scope by going down to DirectorySearcher itself, as it's not possible to set this
        //via PrincipalSearcher directly
        ((DirectorySearcher)oPrincipalSearcher.GetUnderlyingSearcher()).SearchScope = SearchScope.OneLevel;

        List<string> allUsers = new List<string>();

        foreach (var found in oPrincipalSearcher.FindAll())
        {
            allUsers.Add(found.DisplayName.ToString());
        }
        allUsers.Sort();
        return allUsers;
    }

谢谢你的建议!

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2013-07-13 02:33:16

这段代码解决了我的问题:

代码语言:javascript
复制
((DirectorySearcher)oPrincipalSearcher.GetUnderlyingSearcher()).SearchScope = SearchScope.OneLevel;

最终的方法如下所示:

代码语言:javascript
复制
public List<string> getAllActiveUsers()
    {
        PrincipalContext oPrincipalContext = GetPrincipalContext();
        UserPrincipal oUserPrincipal = new UserPrincipal(oPrincipalContext) { Enabled = true };
        PrincipalSearcher oPrincipalSearcher = new PrincipalSearcher(oUserPrincipal);
        //Setting the search scope by going down to DirectorySearcher itself, as it's not possible to set this
        //via PrincipalSearcher directly
        ((DirectorySearcher)oPrincipalSearcher.GetUnderlyingSearcher()).SearchScope = SearchScope.OneLevel;

        List<string> allUsers = new List<string>();

        foreach (var found in oPrincipalSearcher.FindAll())
        {
            allUsers.Add(found.DisplayName.ToString());
        }
        allUsers.Sort();
        return allUsers;
    }
票数 1
EN

Stack Overflow用户

发布于 2013-07-12 02:22:10

嗯,我不熟悉PrincipalSearcher,但您可以尝试使用DirectorySearcher。它有一个选项SearchScope,允许您设置OneLevelSubTree (或Base)。下面是一些您可以尝试的示例代码(这是从我的产品代码中拆分出来的,因此它未经测试,甚至可能无法编译,但它应该会给您一些启发):

代码语言:javascript
复制
DirectorySearcher dsr = new DirectorySearcher();
dsr.SearchRoot = new DirectoryEntry(settings.Path, settings.Username, settings.Password, settings.AuthType);
dsr.PageSize = 100;
dsr.SizeLimit = 0;
dsr.SearchScope = SearchScope.OneLevel;
dsr.Filter = "(&(objectclass=user)(sn=UserLastName))";
dsr.PropertiesToLoad.AddRange(new string[] { "sn", "givenName" });
using (SearchResultCollection src = dsr.FindAll())
{
    foreach (SearchResult sr in src)
    {
        string propName = lp.Name;
        ResultPropertyValueCollection rpvc = sr.Properties[propName];
        string val = (string)rpvc[0];
    }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/17600347

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档