目标
我正在写一个抽象各种Windows用户机制的类。 我的class级知道用户的帐户名称和域名,如果有的话。 我试图水合一个属性,指出用户是否具有pipe理权限的域或本地环境,它属于。
问题
WindowsPrincipal
类通过IsInRole
提供信息,但是它的构造函数需要一个WindowsIdentity
,我无法find一个没有用户主体名称(UPN)的方法。 UserPrincipal.UserPrincipalName
属性可供域用户使用,但本地用户为空。 是否有另一种方法从UserPrincipal
获取WindowsPrincipal
? 另外,还有没有它的另一种方法来完成目标?
来源
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); } } }
提前致谢。