ADのユーザーアカウントが有効になっているかどうかを確認しようとしています。なぜUserPrincipal.Enabledが異なる値を返しますか?
string domain = "my domain";
string group = "my security group";
string ou = "my OU";
//init context
using (var cnt= new PrincipalContext(ContextType.Domain, domain))
{
//find the necessary security group
using (GroupPrincipal mainGroup
= GroupPrincipal.FindByIdentity(cnt, IdentityType.Guid, group))
{
if (mainGroup != null)
{
//get the group's members
foreach (var user in mainGroup.GetMembers()
.OfType<UserPrincipal>()
.Where(u => u.DistinguishedName.Contains(ou)))
{
//ensure that all the info about the account is loaded
//by using FindByIdentity as opposed to GetMembers
var tmpUser= UserPrincipal.FindByIdentity(cnt,
user.SamAccountName);
//actually I could use `user` variable,
//as it gave the same result as `tmpUser`.
//print the account info
Console.WriteLine(tmpUser.Name + "\t" +
tmpUser.Enabled.HasValue + "\t" +
tmpUser.Enabled.Value);
}
}
}
}
私は、管理者アカウントで、このコードを実行すると、私は非特権のアカウントの下でそれを実行するとしながら、私は、本当の結果を得るている問題、user.Enabled
戻りfalse
を:このために私は次のコードを使用します一部のアカウントではtrue
である必要があります。
私は見つけることができただけで同様のQ &は、ここでは役に立たない
- UserPrincipal.Enabled returns False for accounts that are in fact enabled?
- Everything in Active Directory via C#.NET 3.5 (Using System.DirectoryServices.AccountManagement)
です。
なぜそうですか?非公開のアカウントでこの情報を取得するための選択肢は何ですか? How to determine if user account is enabled or disabled:ここ
は別のアプローチである
private bool IsActive(DirectoryEntry de)
{
if (de.NativeGuid == null)
return false;
int flags = (int)de.Properties["userAccountControl"].Value;
if (!Convert.ToBoolean(flags & 0x0002))
return true;
else
return false;
}
同じアプローチはActive Directory Objects and C#に記載されています。
ただし、特権のないユーザーアカウントで実行されている場合、の属性はnull
であり、アカウントの状態を判断することはできません。
ここでこの問題を回避するには、ADにアクセスするための十分なpriviledgesを持つユーザーの資格情報を指定し、PrincipalContext Constructorを使用することです。
私には不明な点があります。なぜなら、非特権ユーザーがADにアクセスできた理由と、特定のアカウント属性の値を取得できなかった理由です。おそらくこれはC#とは関係がなく、ADで設定する必要があります。
リンクから正確な指示を追加することができれば、あなたの答えはリンクが壊れていなくても安全です! – ForceMagic
ありがとうございました。リンクからの関連情報のブロッククォートを追加しました。 –