.NET 4 を使用しているため、System.DirectoryServices.AccountManagement
(S.DS.AM) 名前空間を確認する必要があります。ここでそれについてすべて読んでください:
基本的に、ドメイン コンテキストを定義し、AD でユーザーやグループを簡単に見つけることができます。
// set up domain context
PrincipalContext ctx = new PrincipalContext(ContextType.Domain);
// find a user - by e.g. his "samAccountName", or the Windows user name or something
UserPrincipal user = UserPrincipal.FindByIdentity(ctx, "SomeUserName");
if(user != null)
{
// do something here....
string samAccountName = user.SamAccountName;
}
ユーザー名で指定されたユーザーが見つからない場合は、新しい検索機能を使用することもできます。
// define a "query-by-example" principal - here, we search for a UserPrincipal
// and with the first name (GivenName) and a last name (Surname)
UserPrincipal qbeUser = new UserPrincipal(ctx);
qbeUser.GivenName = firstName;
qbeUser.Surname = lastName;
// create your principal searcher passing in the QBE principal
PrincipalSearcher srch = new PrincipalSearcher(qbeUser);
// find all matches
foreach(var found in srch.FindAll())
{
// do whatever here - "found" is of type "Principal" - it could be user, group, computer.....
}
新しい S.DS.AM を使用すると、AD でユーザーやグループを簡単に操作できます。また、1 人のユーザーを見つけるだけでも、比較的短時間で済みます。