4

Active Directory にユーザーごとに異なる OU があり、C# を使用して特定の OU のすべてのユーザーを取得したいと考えています。

現在、私はこのフィルターを持っていますが、すべての OU からすべてのユーザーを返します

(&(objectClass=User)(objectCategory=Person))

LDAPを使用して特定のユーザーのユーザーを見つけるのを手伝ってください

4

2 に答える 2

7

PrincipalSearcherおよび「例によるクエリ」プリンシパルを使用して検索を行うことができます。

// LDAP string to define your OU
string ou = "OU=Sales,DC=YourCompany,DC=com";

// set up a "PrincipalContext" for that OU
using (PrincipalContext ctx = new PrincipalContext(ContextType.Domain, "Yourcompany.com", ou))
{
    // define the "query-by-example" user (or group, or computer) for your search
    UserPrincipal qbeUser = new UserPrincipal(ctx);

    // set whatever attributes you want to limit your search for, e.g. Name, etc.
    qbeUser.Surname = "Smith";

    // define a searcher for that context and that query-by-example 
    using (PrincipalSearcher searcher = new PrincipalSearcher(qbeUser))
    {
        foreach (Principal p in searcher.FindAll())
        {
            // Convert the "generic" Principal to a UserPrincipal
            UserPrincipal user = p as UserPrincipal;

            if (user != null)
            {
                // do something with your found user....
            }
        }
    }

まだお読みでない場合は、.NET Framework 3.5でディレクトリ セキュリティ プリンシパルを管理するという MSDN の記事を必ずお読みくださいSystem.DirectoryServices.AccountManagement。または、System.DirectoryServices.AccountManagement 名前空間に関する MSDN ドキュメントを参照してください。

もちろん、必要に応じて、作成した「例によるクエリ」ユーザー プリンシパルに他のプロパティを指定することもできます。

  • DisplayName(通常: 名 + スペース + 姓)
  • SAM Account Name- Windows/AD アカウント名
  • User Principal Name- "username@yourcompany.com" スタイル名

の任意のプロパティを指定し、UserPrincipalそれらを の「例によるクエリ」として使用できますPrincipalSearcher

于 2015-01-07T13:00:29.220 に答える