は、.NET 3.5とアップにしている場合は、あなたがSystem.DirectoryServices.AccountManagement
(S.DS.AM)名前空間をチェックアウトする必要があります。ここではそれについてのすべてを読む:
基本的に、あなたはドメインコンテキストを定義し、簡単にADのユーザーおよび/またはグループを見つけることができます:
// set up domain context
PrincipalContext ctx = new PrincipalContext(ContextType.Domain);
// find a user
UserPrincipal user = UserPrincipal.FindByIdentity(ctx, "SomeUserName");
if(user != null)
{
// do something here....
}
// find the group in question
GroupPrincipal group = GroupPrincipal.FindByIdentity(ctx, "YourGroupNameHere");
// if found....
if (group != null)
{
// iterate over members
foreach (Principal p in group.GetMembers())
{
Console.WriteLine("{0}: {1}", p.StructuralObjectClass, p.DisplayName);
// do whatever you need to do to those members
}
}
新しいS.DS.AMにより、ADのユーザーとグループで簡単に遊ぶことができます。
コンピュータアカウントは、(Principal
から派生)と表示されるので、簡単にユーザーとコンピュータアカウントを分け合わせることができます。
S.DS.AMに移動できない、または移動したくない場合は、LDAPフィルタのobjectClassの代わりにobjectCategory
を使用して、ユーザーとコンピュータを分けておくこともできます。 objectCategory
は、索引付けされていて多値ではないため、とにかく有益です。そのため、クエリのパフォーマンスははるかに向上します。
現実のユーザーの場合はobjectCategory = Person
を使用し、コンピュータの場合はLDAPフィルタにobjectCategory = Computer
を使用します。