Posted By

mecha on 05/17/11


Tagged


Versions (?)

Get a Windows Active Directory User Display Name


 / Published in: C#
 

  1. /// <summary>
  2. /// Gets the user AD user display name from Principal Context.
  3. /// </summary>
  4. /// <param name="username">The username.</param>
  5. /// <returns>Returns the display name of AD user, and an empty string if not found.</returns>
  6. public static string GetADUserDisplayName(string username) {
  7. using (var pctx = new PrincipalContext(ContextType.Domain)) {
  8. using (UserPrincipal up = UserPrincipal.FindByIdentity(pctx, username)) {
  9. return up != null && !String.IsNullOrEmpty(up.GivenName) && !String.IsNullOrEmpty(up.Surname) ? string.Format("{0} {1}", up.GivenName, up.Surname) : string.Empty;
  10. }
  11. }
  12. }

Report this snippet  

You need to login to post a comment.