title: C# System.DirectoryServices.AccountManagement - UserPrincipal.FindByIdentity returning NULL for some users
body: <p>I am currently using a method to grab to GivenName and Surname of the current logged in user from AD.  All users accessing this site will be internal users within AD.</p>
  
  <p>For some users this is working and for others it seems that the UserPrincipal is returning NULL when trying to FindByIdentity.  Is this an OU thing?  I do not specify OUs to look in, I just want it to look for the current logged in user by their login name and pull their First/Last name no matter where they are in the AD structure...</p>
  
  <p>Below is the code for my method...  It takes in a param containing the current user's "User.Identity.Name"...</p>
  
  <pre><code>public static string GetFullName(string userIdentityName)
  {            
      using (var context = new PrincipalContext(ContextType.Domain))
      {
          using (UserPrincipal principal = UserPrincipal.FindByIdentity(context, userIdentityName))
          {
              return principal != null ? principal.GivenName + " " + principal.Surname : "User Not Found";
          }
      }
  }
  </code></pre>
  
  <p>I also output User.Identity.Name on the page and that works for everyone so I know the param passed into the method is populated.</p>
  
  <p>What is odd is I can hard-code a users identity name (that normally returns a null) and the code works even though the exact same thing is being passed through the param otherwise.  Something like:</p>
  
  <pre><code>public static string GetFullName(string userIdentityName)
  {
      var username = "DOMAIN\\username";
  
      using (var context = new PrincipalContext(ContextType.Domain))
      {
          using (UserPrincipal principal = UserPrincipal.FindByIdentity(context, username))
          {
              return principal != null ? principal.GivenName + " " + principal.Surname : "User Not Found";
          }
      }
  }
  </code></pre>
  
  <p>I don't get it...  Any help would be GREATLY appreciated!  Thank you!!</p>