title: How to get a username in Active Directory from a display name in C#?
body: <p>I want to be able to obtain the userid of a user in Active Directory using the display name of that user. The display name is obtained from a database, and has been stored during that user's session using the following code to obtain the display name:</p>
  
  <pre><code>using System.DirectoryServices.AccountManagement;
  
      private string GetDisplayName()
      {
          // set up domain context
          PrincipalContext ctx = new PrincipalContext(ContextType.Domain);
  
          // find currently logged in user
          UserPrincipal user = UserPrincipal.Current;
  
          return user.DisplayName;
      }
  </code></pre>
  
  <p>This time around, I would like to have a method named <code>GetUserIdFromDisplayName()</code> that returns the Active Directory login name. Any ideas?</p>