• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 771
  • Last Modified:

Get Full Display Name from Windows Logon in .Net

I've tried many variations, but based on the following code snippet do you see how I could get the full users display name from the windows logon?  I have NO experience with .NET.  My attempts have been via standard vb script without success:

      Protected Sub Page_PreInit(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.PreInit
          Integer.TryParse(Request.QueryString("forumid"), RequestedForumID)
               If Not User.Identity.IsAuthenticated Then

                Dim strUsrNm As String    
               'Build a string containing the AD account name of the Windows Authenticated user         
                strUsrNm = Request.ServerVariables("LOGON_USER")
                  strUsrNm = Mid$(strUsrNm, InStr(1, strUsrNm, "\") + 1)

               'Create the authentication ticket and add it to the cookie collection
                FormsAuthentication.SetAuthCookie(strUsrNm, False)
               'refresh the current page so the it displays Welcome <username>
            End If
      End Sub
  • 4
  • 3
1 Solution
So are you using FormsAuthentication  or Windows?
You can check that in your web.config file.
Also can you explain with example what you mean with full display name
TCCIRMAuthor Commented:
I will be using Windows authentication.  Right now the current code retrieves the Windows logon name and I want to get the full diplay name.  I've been able to get the full display name from Active Directory in other apps via classic asp like below but have been unable to entirely adapt it to .net.  Above is a code snippet from a .net app I am trying to modify.

     Dim strLogonUser
     Dim strDomain
     Dim strLogonName
     strLogonUser = Request.ServerVariables("Logon_User")
     strDomain = Mid(strLogonUser, 1, Instr(1, strLogonUser, "\") - 1)
     strLogonName = Mid(strLogonUser, Instr(1, strLogonUser, "\") + 1)

    Set dUser = GetObject("WinNT://" & strDomain & "/" & strLogonName & "")
    Display = dUser.Fullname
     Function GetUserFullName(strDomainName, strLogonName)
    End Function
Basicall you will use classes in  System.DirectoryServices namespace.

See if these helps:

Step 16: http://www.vbdotnetheaven.com/UploadFile/ecabral/ADandNETInVB11112005020216AM/ADandNETInVB.aspx

You will need to set LDAP string accordingly.
Free Backup Tool for VMware and Hyper-V

Restore full virtual machine or individual guest files from 19 common file systems directly from the backup file. Schedule VM backups with PowerShell scripts. Set desired time, lean back and let the script to notify you via email upon completion.  

TCCIRMAuthor Commented:
Sorry for the delayed response but after much toil, it is apparent I do not have permissions to access our networks LDAP server thus I am unable to utilize this method.  Any other recommendations how I might retrieve the users full display name?  Thanks
TCCIRMAuthor Commented:
I guess my request is not possible.  Thanks anyway....
Sorry I missed your previous response. It is not that only your account should have access but you can ask your IT or AD Admin to give you appropriate connectionstring to LDAP.
I am not sure there is any other way for getting user details without querying LDAP.
TCCIRMAuthor Commented:

Unfortunately I am at the bottom of the food chain and will not be given access to LDAP.  I appreciate the initial response.  The points are yours.  I'll give this one a rest and possibly revisit it another time.  Thanks  

Featured Post

Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now