title: UserPrincipal from Active Directory
body: <p>I have problem with getting UserPrincipal from Active Directory. First of all I have used on my local environment (using not IIS but ASP.NET development Server):</p>
  
  <pre><code>User usr = new User();
  usr.SoeId = Request.ServerVariables["LOGON_USER"];
  usr.IP = Request.ServerVariables["REMOTE_ADDR"];
  usr.FirstName = UserPrincipal.Current.GivenName;
  usr.LastName = UserPrincipal.Current.Surname;
  </code></pre>
  
  <p>And it works fine. I got what I want. But when I install application on testing environment I got error "Object reference not set to an instance of an object". I have tried solution from <a href="https://stackoverflow.com/questions/3471635/get-first-name-last-name-of-logedin-windows-user">here</a>.</p>
  
  <pre><code>using (PrincipalContext pc = new PrincipalContext(ContextType.Domain))
  {
      UserPrincipal up = UserPrincipal.FindByIdentity(pc, usr.SoeId);
      return up.DisplayName;
      // or return up.GivenName + " " + up.Surname;
  }
  </code></pre>
  
  <p>But it does not work.</p>
  
  <p>I use windows authentication. Impersonation is set to true. Please help me. </p>