Get the Current User's Display Name

In an ASP.Net page you can easily get the username of the currently logged in user with Request.ServerVariables(“AUTH_USER”). With the following code snippet you can also get their real name (the “Display Name”) from the Active Directory.

To use the following code you will need to be using .NET 2, access to the page in IIS must be controlled via Windows Authentication, and there must be a reference to the System.DirectoryServices in the Web.Config file. The function returns a string with the Display Name of the currently logged in user. This code is written in VB.NET.

 1Function GetUserFullName() As String
 2 'Returns the Full Name (Display Name) of the domain user accessing this web page
 3 'Requires the User to be logged in using Windows Authentication
 4 'Also requires a reference to System.DirectoryServices (.NET 2)
 5 Try
 6  Dim dEntry As System.DirectoryServices.DirectoryEntry = New System.DirectoryServices.DirectoryEntry("WinNT://" + Request.ServerVariables("AUTH_USER").Replace("\", "/"))
 7  Return dEntry.Properties("fullName").Value.ToString
 8 Catch
 9  Return ""
10 End Try
11End Function