Get a Windows Active Directory User Email Address


/ Published in: C#
Save to your folder(s)



Copy this code and paste it in your HTML
  1. /// <summary>
  2. /// Gets the user AD email address from Principal Context.
  3. /// </summary>
  4. /// <param name="username">The username.</param>
  5. /// <returns>Returns the email address of AD user, and an empty string if not found.</returns>
  6. public static string GetADUserEmailAddress(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.EmailAddress) ? up.EmailAddress : string.Empty;
  10. }
  11. }
  12. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.