Solved

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

Posted on 2011-03-10
7
272 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
[X]
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
  • 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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…
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

763 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