c# - Get WindowsPrincipal from UserPrincipal -
the goal
i'm writing class abstracts various windows user mechanics. class knows user's account name , domain, if any. trying hydrate property indicates whether user has administrative privilege on either domain or local environment belongs to.
the problem
the windowsprincipal
class provides information via isinrole
, it's constructor requires windowsidentity
, can't find way establish without user principal name (upn). userprincipal.userprincipalname
property available domain users, null local users. there way windowsprincipal
userprincipal
? alternatively, there way accomplish goal without it?
the source
using (principalcontext principalcontext = new principalcontext(principalcontexttype, principalcontextname)) { using (userprincipal userprincipal = userprincipal.findbyidentity(principalcontext, identitytype.name, name)) { // capture additional information user principal. certificates = userprincipal.certificates; displayname = userprincipal.displayname; userprincipalname = userprincipal.userprincipalname; // constructor blows because userprincipalname null local users. using (windowsidentity windowsidentity = new windowsidentity(userprincipalname)) { // capture group membership information specified user. windowsprincipal windowsprincipal = new windowsprincipal(windowsidentity); // determine if user has administrative privilege on domain or local machine. hasadministrativeprivilege = windowsprincipal.isinrole(windowsbuiltinrole.administrator); } } }
thanks in advance.
Comments
Post a Comment