System.DirectoryServices.AccountManagement.dll
「Domain Users」グループ内のすべてのユーザーを取得するために Active Directory を処理するために使用しています。
これはドメイン内のすべてのユーザーを返していますが、有効なユーザーだけを取得する必要があります。
サンプルコードは次のとおりです。
List<string> users = new List<string>();
PrincipalContext pcContext = GetPrincipalContext();
GroupPrincipal grp = GroupPrincipal.FindByIdentity(pcContext,
IdentityType.Name,
"Domain Users");
foreach (Principal user in grp.GetMembers(true).OfType<UserPrincipal>())
{
if (user.Enabled != false)
{
users.Add(user.Name);
}
}
他のグループは正常に機能しますが、グループが "Domain Users" の場合、Enabled
プロパティの値はfalse
すべてのユーザーを対象としています。これにより、各ユーザーに対してさらにクエリを実行しないと、有効なユーザーと無効なユーザーを区別することができなくなります。