Get Full Display Name from Windows Logon in .Net

Posted on 2010-01-08
Last Modified: 2012-06-21
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
Question by:TCCIRM
    LVL 41

    Expert Comment

    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

    Author Comment

    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
    LVL 41

    Accepted Solution

    Basicall you will use classes in  System.DirectoryServices namespace.

    See if these helps:
    Step 16:

    You will need to set LDAP string accordingly.

    Author Comment

    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

    Author Comment

    I guess my request is not possible.  Thanks anyway....
    LVL 41

    Expert Comment

    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.

    Author Comment


    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

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Join & Write a Comment

    Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
    When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
    Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
    This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

    734 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    19 Experts available now in Live!

    Get 1:1 Help Now