3

特定の AD グループのユーザーを取得するにはどうすればよいですか?

ドメイン、ユーザー名、およびパスワードを使用して PrincipalContext をインスタンス化することから始めますか?

4

3 に答える 3

16

まず、グループを見つけます。次に、GetMembers() を使用してユーザーを列挙します。

using (var context = new PrincipalContext( ContextType.Domain ))
{
     using (var group = GroupPrincipal.FindByIdentity( context, "groupname" ))
     {
           var users = group.GetMembers( true ); // recursively enumerate
           ...
     }
}

.NET 4.0 で修正されたバグがあり、グループの 1500 を超えるメンバーの列挙に失敗することに注意してください。大規模なグループがある場合は、System.DirectoryServices の古いメソッドを利用する別の方法を使用する必要があります。

于 2010-01-20T13:34:33.347 に答える
4

この記事「Managing Directory Security Principals in the .NET Framework 3.5 」を参照して、.NET 3.5 でできることの概要を確認してくださいSystem.DirectoryServices.AccountManagement

グループのメンバーを取得するには、次のようにします。

// build the principal context - use the NetBIOS domain name
PrincipalContext ctx = new PrincipalContext(ContextType.Domain, "DOMAIN");

// get the group you're interested in
GroupPrincipal group = GroupPrincipal.FindByIdentity("cn=YourGroupname");

// iterate over its members
foreach(Principal p in group.Members)
{
    // do whatever you need to do to its members here            
}

お役に立てれば!

于 2010-01-20T13:34:01.070 に答える