目標
さまざまな Windows ユーザー メカニクスを抽象化するクラスを作成しています。私のクラスは、ユーザーのアカウント名とドメインがあれば知っています。ユーザーが属するドメインまたはローカル環境の管理者権限をユーザーが持っているかどうかを示すプロパティをハイドレートしようとしています。
問題
WindowsPrincipal
クラスは を介してその情報を提供しますがIsInRole
、コンストラクターには が必要WindowsIdentity
です。これは、ユーザー プリンシパル名 (UPN) なしで確立する方法が見つかりません。ドメイン ユーザーはUserPrincipal.UserPrincipalName
プロパティを使用できますが、ローカル ユーザーは null です。WindowsPrincipal
からを取得する別の方法はありUserPrincipal
ますか? または、それなしで目標を達成する別の方法はありますか?
起源
using (PrincipalContext principalContext = new PrincipalContext(PrincipalContextType, principalContextName))
{
using (UserPrincipal userPrincipal = UserPrincipal.FindByIdentity(principalContext, IdentityType.Name, Name))
{
// Capture additional information from the user principal.
Certificates = userPrincipal.Certificates;
DisplayName = userPrincipal.DisplayName;
UserPrincipalName = userPrincipal.UserPrincipalName;
// This constructor blows up because UserPrincipalName is null for local users.
using (WindowsIdentity windowsIdentity = new WindowsIdentity(UserPrincipalName))
{
// Capture group membership information about the specified user.
WindowsPrincipal windowsPrincipal = new WindowsPrincipal(windowsIdentity);
// Determine if the user has administrative privilege on the domain or local machine.
HasAdministrativePrivilege = windowsPrincipal.IsInRole(WindowsBuiltInRole.Administrator);
}
}
}
前もって感謝します。