Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 280
  • Last Modified:

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.
0
dbbishop
Asked:
dbbishop
  • 4
  • 2
2 Solutions
 
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
 
Todd GerbertIT 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
 
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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
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 GerbertIT 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

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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.

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