Quantcast
Channel: VBForums - Visual Basic .NET
Viewing all articles
Browse latest Browse all 27210

Active Directory Search to get users domain

$
0
0
Hello all,
I am trying to query Active directory for a user to get a list of details:
1. First/Last Name
2. Email
3. UserName
4. Domain

I am able to get all except for the domain name. Here is my code:

Code:

        Dim oroot As DirectoryEntry = New DirectoryEntry("GC://ldap.someCompany.com")
        Dim osearcher As DirectorySearcher = New DirectorySearcher(oroot)
        Dim result As SearchResult

        osearcher.Filter = String.Format("(&(SAMAccountName={0}))", "myUsername")
        osearcher.PropertiesToLoad.Add("cn")
        osearcher.PropertiesToLoad.Add("SAMAccountName")  'Users login name 
        osearcher.PropertiesToLoad.Add("givenName")    'Users first name 
        osearcher.PropertiesToLoad.Add("sn")  'Users sur name 
        osearcher.PropertiesToLoad.Add("mail")  'Email address

        result = osearcher.FindOne

        Try
            myUser.UserID = result.Properties("cn").Item(0)
            myUser.Domain = ""
            myUser.EmailAddress = result.Properties("mail").Item(0)
            myUser.FirstName = result.Properties("givenName").Item(0)
            myUser.LastName = result.Properties("sn").Item(0)
            myUser.Domain = result.Properties("displayName").Item(0)

        Catch ex As Exception
            Return Nothing
        End Try

This returns this string: GC://ldap.someCompany.com/CN=FirstName M LastName,OU=Employees,OU=Domain Users,DC=val1,DC=val2,DC=com

So, I have two questions. First, how do I get the domain name of the user? It appears to be in DC= (where I have val1)
Second, is there anyway to speed this search up? Right now, it takes about 10 seconds to run the query.

Thanks for any help

jason

Viewing all articles
Browse latest Browse all 27210

Trending Articles