Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

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

Posted on 2011-03-10
7
271 Views
Last Modified: 2012-05-11
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
Comment
Question by:dbbishop
  • 4
  • 2
7 Comments
 
LVL 2

Expert Comment

by:ratstud
ID: 35099980
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
 
LVL 33

Accepted Solution

by:
Todd Gerbert earned 500 total points
ID: 35100158
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
 
LVL 15

Author Comment

by:dbbishop
ID: 35100653
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.

 
LVL 15

Author Comment

by:dbbishop
ID: 35101047
Got it going. Added a reference to it. Thanks.
0
 
LVL 15

Author Comment

by:dbbishop
ID: 35101359
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
 
LVL 15

Author Comment

by:dbbishop
ID: 35101363
If you can just point me to a link that gives the properties, that would be great. Thanks.
0
 
LVL 33

Assisted Solution

by:Todd Gerbert
Todd Gerbert earned 500 total points
ID: 35108801
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: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

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.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

856 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