-
Apr 26th, 2010, 09:41 AM
#1
Thread Starter
Frenzied Member
[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?
-
Apr 26th, 2010, 10:24 AM
#2
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 10:28 AM.
-
Apr 26th, 2010, 10:57 AM
#3
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:
'Import System.DirectoryServices at the top of your class
'-------------
'Function definition
Private Function GetRealNameFromAd(ByVal UsernameToFind As String) As String
Using searcher As New DirectorySearcher(New DirectoryEntry())
searcher.PageSize = 1000
searcher.SearchScope = SearchScope.Subtree
searcher.Filter = "(&(samAccountType=805306368)(sAMAccountName=" & UsernameToFind & "))"
Using Results As SearchResultCollection = searcher.FindAll
If Results Is Nothing OrElse Results.Count <> 1 Then
Throw New ApplicationException("Invalid number of results returned - either no users were found or more than one user account was found")
End If
Using UserDE As DirectoryEntry = Results(0).GetDirectoryEntry
Return CStr(UserDE.Properties("givenName").Value) & " " & CStr(UserDE.Properties("sn").Value)
End Using
End Using
End Using
End Function
'--------------
'Example of using it:
Try
MessageBox.Show("Real name for " & Environment.Username & " is " & GetRealNameFromAd(Environment.Username), "Real Name Found", MessageBoxButtons.OK, MessageBoxIcon.Information)
Catch ex As Exception
MessageBox.Show("The following error was encountered during the Active Directory search: " & ex.Message, "Error During Search", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
-
Apr 26th, 2010, 11:43 AM
#4
Thread Starter
Frenzied Member
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?
-
Apr 26th, 2010, 04:14 PM
#5
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" ?
-
Apr 26th, 2010, 05:26 PM
#6
Thread Starter
Frenzied Member
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.
-
Apr 27th, 2010, 02:39 AM
#7
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?
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|