4

目標

さまざまな 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);
        }
    }
}

前もって感謝します。

4

0 に答える 0