特定のユーザーがローカル管理者グループのメンバーであるかどうかを確認する必要があるプロセスがあります。
それをチェックするコードは次のようになります。
using (PrincipalContext context = new PrincipalContext(ContextType.Machine, null))
{
UserPrincipal user = UserPrincipal.FindByIdentity(context, IdentityType.SamAccountName, sUserName);
if (user != null)
{
SecurityIdentifier adminsGroupSID = new SecurityIdentifier(WellKnownSidType.BuiltinAdministratorsSid, null);
GroupPrincipal group = GroupPrincipal.FindByIdentity(context, IdentityType.Sid, adminsGroupSID.Value);
if (group != null)
{
if (user.IsMemberOf(group))
return 0;
}
}
}
グループに削除されたアカウント (ドメイン アカウントなど) がある場合、PrincipalOperationException が発生し、「グループ メンバーシップの列挙中にエラー (1332) が発生しました。メンバーの SID を解決できませんでした。」というメッセージが表示されます。
これを克服する方法はありますか: a) 孤立した SID をグループから手動で削除する b) 無視しない?
ありがとう