1

私は最近、職場での要件のために C# の学習を開始しました。Active Directory 内の特定の OU をクエリし、その OU のみを照会し、サブ OU を照会しないメソッドを作成しようとしています。メソッドは次のようになります。

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

現在の方法では、ユーザーが有効なユーザー アカウントのみをプルしますが、問題はサブ OU のアカウントをプルすることであり、望ましくありません。コードを変更する提案があれば、最終的な方法がどのようになるかを教えてください。

ありとあらゆる助けをいただければ幸いです。

ありがとう!

更新: どうやら私は十分に Google 検索を行っていなかったようです。いくつかの追加の調査により、必要なものが得られました。更新された方法は次のとおりです。

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

提案をありがとう!

4

2 に答える 2

1

このコードは、私の問題を解決したものです:

((DirectorySearcher)oPrincipalSearcher.GetUnderlyingSearcher()).SearchScope = SearchScope.OneLevel;

最終的な方法は次のようになります。

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;
    }
于 2013-07-12T18:33:15.887 に答える
0

うーん、私は に慣れていませんが、代わりにPrincipalSearcher使用してみてください。または(または)のいずれかを設定できるDirectorySearcherオプションがあります。これはあなたが試すことができるいくつかのサンプルコードです (これは私の製品コードからマングルされているため、テストされておらず、コンパイルさえできないかもしれませんが、アイデアが得られるはずです):SearchScopeOneLevelSubTreeBase

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];
    }
}
于 2013-07-11T18:22:09.743 に答える