Get a users name (last and first) from logon ID

I have an application that allows an administrator to add authorized users. They enter the user ID used to log into Windows, along with the user's first and last name.

I am certain there is probably a way to validate the userid and return the name, because when you set up a shared drive on your own computer, you can add the user ID, and click [Check Names] button and it will return the name. For example, I enter my ID (dbb####) and click Check Names and it returns "Douglas B Bishop (dbb####@domain.Intranet)".
Is there some way I can emulate this operation in VB.Net (2005), where I pass a userid (and possibly domain name) and it returns the name?

I would also like to use this method to iterate through the users who have been set up and be able to validate tha it is still a valid userid (i.e. the employee has not left the company) as users are spread out all over the country.

Thanks for the help.
LVL 15
dbbishopAsked:
Who is Participating?
 
Todd GerbertConnect With a Mentor IT ConsultantCommented:
Add a reference to System.DirectoryServices, include Imports System.DirectoryServices at the top of your code file.

Here's a simple example:
Imports System.DirectoryServices
Module Module1

    Sub Main()
        Dim user1, user2 As DirectoryEntry

        user1 = GetUser("tgerbert")
        If Not IsNothing(user1) Then
            Console.WriteLine("First name: " & user1.Properties("givenName").Value)
        Else
            Console.WriteLine("User tgerbert not found.")
        End If

        user2 = GetUser("bubba", "LDAP://DC=yourdomain,DC=com")
        If Not IsNothing(user2) Then
            Console.WriteLine("Last name: " & user2.Properties("sn").Value)
        Else
            Console.WriteLine("User bubba not found.")
        End If

        Console.ReadKey()
    End Sub

    Public Function GetUser(ByVal userName As String, Optional ByVal domainNameLdapPath As String = Nothing) As DirectoryEntry
        Dim searcher As DirectorySearcher
        Dim result As SearchResult

        If String.IsNullOrEmpty(domainNameLdapPath) Then
            searcher = New DirectorySearcher()
        Else
            searcher = New DirectorySearcher(New DirectoryEntry(domainNameLdapPath))
        End If

        searcher.Filter = String.Format("(sAMAccountName={0})", userName)
        result = searcher.FindOne()

        If IsNothing(result) Then
            Return Nothing
        Else
            Return result.GetDirectoryEntry()
        End If
    End Function
End Module

Open in new window

0
 
ratstudCommented:
You will have to do LDAP queries to the active directory to get these attributes from the userid... hope this helps point you in the right direction.
0
 
dbbishopAuthor Commented:
tgerbert: I thought I was in heaven for a moment. I copied this code, but am getting the following error:

Namespace or type specified in the Imports 'System.DirectoryServices' doesn't contain any public
member or cannot be found. Make sure the namespace or the type is defined and contains at least one
public member. Make sure the imported element name doesn't use any aliases.

Open in new window


Any suggestions?
0
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
dbbishopAuthor Commented:
Got it going. Added a reference to it. Thanks.
0
 
dbbishopAuthor Commented:
I have been looking everywhere for information about the properties of the DirectoryEntry object. From this sample, I know that "givenName" is the first name and "sn" is the last name. What other properties are available (for instance, location or phone number, etc. These would be valuable for tracking down an employee if there were questions. I could build in a functin where the administrator could double-click on an entry and I could bring up information regarding them.
0
 
dbbishopAuthor Commented:
If you can just point me to a link that gives the properties, that would be great. Thanks.
0
 
Todd GerbertConnect With a Mentor IT ConsultantCommented:
I usually look in adsiedit.msc (http://technet.microsoft.com/en-us/library/cc773354(WS.10).aspx) to see available properties.  You can also just loop through the properties on one of your users:

Imports System.DirectoryServices
Module Module1

    Sub Main()
        Dim user = GetUser("tgerbert")

        For Each prop As PropertyValueCollection In user.Properties
            Console.WriteLine("{0}: {1}", prop.PropertyName, prop.Value)
        Next

        Console.ReadKey()
    End Sub

    Public Function GetUser(ByVal userName As String, Optional ByVal domainNameLdapPath As String = Nothing) As DirectoryEntry
        Dim searcher As DirectorySearcher
        Dim result As SearchResult

        If String.IsNullOrEmpty(domainNameLdapPath) Then
            searcher = New DirectorySearcher()
        Else
            searcher = New DirectorySearcher(New DirectoryEntry(domainNameLdapPath))
        End If

        searcher.Filter = String.Format("(sAMAccountName={0})", userName)
        result = searcher.FindOne()

        If IsNothing(result) Then
            Return Nothing
        Else
            Return result.GetDirectoryEntry()
        End If
    End Function
End Module

Open in new window

0
All Courses

From novice to tech pro — start learning today.