8

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すべてのユーザーを対象としています。これにより、各ユーザーに対してさらにクエリを実行しないと、有効なユーザーと無効なユーザーを区別することができなくなります。

4

3 に答える 3

1

UserPrinciple オブジェクトには、このための bool Enabled プロパティがあります。

http://msdn.microsoft.com/en-us/library/system.directoryservices.accountmanagement.userprincipal_properties.aspx

// Add this to GetUserDetails
objUserDetails.EmployeeId = UserPrinical.EmployeeId;


// Then condition the add to only add enabled
if (objUserDetails.Enabled) {
    objUserDetails.Add(GetUserDetails(p.Name));
}
于 2013-01-18T19:38:08.513 に答える
1

この問題を回避する方法は、最初にPrincipalSearcherクラスを使用して有効なユーザーを検索し、次にプリンシパルのメソッドを使用することです。IsMemberOf()

List<string> users = List<string>();
PrincipalContext pcContext = GetPrincipalContext();
GroupPrincipal grp = GroupPrincipal.FindByIdentity(pcContext, IdentityType.Name, "Domain Users");
UserPrincipal searchFilter = new UserPrincipal(pcContext){ Enabled = true }
PrincipalSearcher searcher = new PrincipalSearcher(searchFilter);
PrincipalSearchResult<Principal> results = searcher.FindAll();
foreach (Principal user in results)
    if (user.IsMemberOf(grp))
        users.Add(user.SamAccountName);
于 2015-12-21T02:57:45.190 に答える
0

Enabled プロパティのMSDN ページに次のようなコメントがあります。

プリンシパルがストアに保存されていない場合、このプロパティは null を返します。プリンシパルが永続化された後、デフォルトで有効になる設定はストアによって異なります。AD DS ストアと AD LDS ストアでは、新しいプリンシパルが永続化されると無効になりますが、SAM では新しいプリンシパルが永続化されると有効になります。アプリケーションは、ストアに永続化された後にのみ、このプロパティに値を設定できます。

デフォルトが false の場合、おそらく関連していますか?

また、MSDN フォーラムには、実際に有効になっているアカウントに対して UserPrincipal.Enabled が False を返すという投稿がありますか? それは本当にあなたの問題に似ています。投稿によると、おそらくここに解決策があります:

私は誤解したと思います。前に投稿したものは無視してください。私は何が起こっているのか知っていると思います。GetMembers メソッドは明らかに UserPrincipal データをロードしていません。より良い解決策があるかどうかはわかりませんが、次の方法で機能します(少なくとも私のADでは):

foreach (UserPrincipal user in group.GetMembers(false))
{
   UserPrincipal tempUser = UserPrincipal.FindByIdentity(context, user.SamAccountName);
   // use tempUser.Enabled
   // other code here
}
于 2013-08-11T01:28:29.387 に答える