特定の AD グループのユーザーを取得するにはどうすればよいですか?
ドメイン、ユーザー名、およびパスワードを使用して PrincipalContext をインスタンス化することから始めますか?
特定の AD グループのユーザーを取得するにはどうすればよいですか?
ドメイン、ユーザー名、およびパスワードを使用して PrincipalContext をインスタンス化することから始めますか?
まず、グループを見つけます。次に、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 の古いメソッドを利用する別の方法を使用する必要があります。
この記事「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
}
お役に立てれば!