Solved

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

Posted on 2011-03-10
7
268 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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

743 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now