title: Get WindowsPrincipal from UserPrincipal
body: <p><strong>The goal</strong></p>
  
  <p>I'm writing a class that abstracts various Windows user mechanics. My class knows about the user's account name and domain, if any. I am trying to hydrate a property that indicates whether the user has administrative privilege on either the domain or the local environment that it belongs to.</p>
  
  <p><strong>The problem</strong></p>
  
  <p>The <code>WindowsPrincipal</code> class provides that information via <code>IsInRole</code>, but it's constructor requires a <code>WindowsIdentity</code>, which I can't find a way to establish without a user principal name (UPN). The <code>UserPrincipal.UserPrincipalName</code> property is available for domain users, but null for local users. Is there another way to get a <code>WindowsPrincipal</code> from a <code>UserPrincipal</code>? Alternatively, is there another way to accomplish the goal without it?</p>
  
  <p><strong>The source</strong></p>
  
  <pre><code>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);
          }
      }
  }
  </code></pre>
  
  <p>Thanks in advance.</p>