私は最近、職場での要件のために C# の学習を開始しました。Active Directory 内の特定の OU をクエリし、その OU のみを照会し、サブ OU を照会しないメソッドを作成しようとしています。メソッドは次のようになります。
public List<string> getAllActiveUsers()
{
PrincipalContext oPrincipalContext = GetPrincipalContext();
UserPrincipal oUserPrincipal = new UserPrincipal(oPrincipalContext)
{
Enabled = true
};
PrincipalSearcher oPrincipalSearcher = new PrincipalSearcher(oUserPrincipal);
List<string> allUsers = new List<string>();
foreach (var found in oPrincipalSearcher.FindAll())
{
allUsers.Add(found.DisplayName.ToString());
}
allUsers.Sort();
return allUsers;
}
現在の方法では、ユーザーが有効なユーザー アカウントのみをプルしますが、問題はサブ OU のアカウントをプルすることであり、望ましくありません。コードを変更する提案があれば、最終的な方法がどのようになるかを教えてください。
ありとあらゆる助けをいただければ幸いです。
ありがとう!
更新: どうやら私は十分に Google 検索を行っていなかったようです。いくつかの追加の調査により、必要なものが得られました。更新された方法は次のとおりです。
public List<string> getAllActiveUsers()
{
PrincipalContext oPrincipalContext = GetPrincipalContext();
UserPrincipal oUserPrincipal = new UserPrincipal(oPrincipalContext) { Enabled = true };
PrincipalSearcher oPrincipalSearcher = new PrincipalSearcher(oUserPrincipal);
//Setting the search scope by going down to DirectorySearcher itself, as it's not possible to set this
//via PrincipalSearcher directly
((DirectorySearcher)oPrincipalSearcher.GetUnderlyingSearcher()).SearchScope = SearchScope.OneLevel;
List<string> allUsers = new List<string>();
foreach (var found in oPrincipalSearcher.FindAll())
{
allUsers.Add(found.DisplayName.ToString());
}
allUsers.Sort();
return allUsers;
}
提案をありがとう!