[RESOLVED] Convert environment username to LDAP First Name, Last Name-VBForums
Results 1 to 7 of 7

Thread: [RESOLVED] Convert environment username to LDAP First Name, Last Name

  1. #1

    Thread Starter
    Frenzied Member stateofidleness's Avatar
    Join Date
    Jan 2009
    Posts
    1,780

    Resolved [RESOLVED] Convert environment username to LDAP First Name, Last Name

    Hey Guys,
    In an application I'm working on I am using the Environment.UserName Property to get the logged on user's username. Is there a way to convert this to the logged on user's REAL first name and last name that is associated with it in Active Directory?

    I'd like to avoid having to connect or query Active Directory directly if possible (as this is a corporate domain and I'm not exactly the IS guy )

    I also wanted to avoid hard-coding lookups into the app itself, as the users of the application will change over time.

    Would a "users" external file (Text, XML, csv) be better for this?

  2. #2
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    Re: Convert environment username to LDAP First Name, Last Name

    Unfortunately there is no local environmental variable that holds the user's first and last name and the only way I know of to do it is to query AD. Its pretty simple to do though when you have their username, just add a reference to System.DirectoryServices and then you can use the DirectorySearcher class to search for any accounts with a username that matches the username you got from Environment.Username, then you can either grab the name attribute, or the givenName (first name) and sn (surname) attributes and combine them. I can write you an example if you want

    PS glad to see your website/blog is back up and running
    Last edited by chris128; Apr 26th, 2010 at 11:28 AM.
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  3. #3
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    Re: Convert environment username to LDAP First Name, Last Name

    OK so I figured a few other people might find this useful so I've written a little example anyway:

    vb Code:
    1. 'Import System.DirectoryServices at the top of your class
    2.  
    3. '-------------
    4. 'Function definition
    5. Private Function GetRealNameFromAd(ByVal UsernameToFind As String) As String
    6.         Using searcher As New DirectorySearcher(New DirectoryEntry())
    7.             searcher.PageSize = 1000
    8.             searcher.SearchScope = SearchScope.Subtree
    9.             searcher.Filter = "(&(samAccountType=805306368)(sAMAccountName=" & UsernameToFind & "))"
    10.             Using Results As SearchResultCollection = searcher.FindAll
    11.                 If Results Is Nothing OrElse Results.Count <> 1 Then
    12.                     Throw New ApplicationException("Invalid number of results returned - either no users were found or more than one user account was found")
    13.                 End If
    14.                 Using UserDE As DirectoryEntry = Results(0).GetDirectoryEntry
    15.                     Return CStr(UserDE.Properties("givenName").Value) & " " & CStr(UserDE.Properties("sn").Value)
    16.                 End Using
    17.             End Using
    18.         End Using
    19. End Function
    20.  
    21. '--------------
    22. 'Example of using it:
    23. Try
    24.      MessageBox.Show("Real name for " & Environment.Username & " is " & GetRealNameFromAd(Environment.Username), "Real Name Found", MessageBoxButtons.OK, MessageBoxIcon.Information)
    25. Catch ex As Exception
    26.      MessageBox.Show("The following error was encountered during the Active Directory search: " & ex.Message, "Error During Search", MessageBoxButtons.OK, MessageBoxIcon.Error)
    27. End Try
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  4. #4

    Thread Starter
    Frenzied Member stateofidleness's Avatar
    Join Date
    Jan 2009
    Posts
    1,780

    Re: Convert environment username to LDAP First Name, Last Name

    damn chris that's awesome!
    after I posted, I started implementing the text file route and got it working pretty easily, but I will definitely give this a go here in a sec!

    do you think there will be any adverse affects on the LDAP servers or anything with that code or is it basically doing everything host-side?

  5. #5
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    Re: [RESOLVED] Convert environment username to LDAP First Name, Last Name

    do you think there will be any adverse affects on the LDAP servers or anything with that code or is it basically doing everything host-side?
    No none at all, queries like this happen hundreds of times a day in a large network and it is exactly what an LDAP server is designed to do. I do far more resource intensive querries in some of my programs and have never seen any problems arise from it.

    PS I'm curious what you mean when you say "the text file route" ?
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  6. #6

    Thread Starter
    Frenzied Member stateofidleness's Avatar
    Join Date
    Jan 2009
    Posts
    1,780

    Re: [RESOLVED] Convert environment username to LDAP First Name, Last Name

    I have a text file with the environment name (the logon id) and the corresponding name
    I have a team of about 11 people so it isn't that many

    d123456
    John Doe
    j654321
    Jane Johnson

    etc...

    So i just read in the text file and compare the name to the environment name
    works great! also allows me to update the text file whenever i have employees change.

    the active directory is nice, but our company tends to lag when updating it.

  7. #7
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    Re: [RESOLVED] Convert environment username to LDAP First Name, Last Name

    ahh I see, fair enough then maybe that AD code will help someone else out in the future anyway.

    Surely your IT guys create user accounts for people as soon as someone starts with the company though?
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width

Survey posted by VBForums.