Return to Snippet

Revision: 46298
at May 17, 2011 03:50 by mecha


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

Initial URL


Initial Description


Initial Title
Get a Windows Active Directory User Display Name

Initial Tags


Initial Language
C#