How to get user information from Active Directory?

  • Thread starter Thread starter DotNetJunkies User
  • Start date Start date
D

DotNetJunkies User

Does anyone know how to get the user information such as Name, Address, Email, Phone Number, etc from Windows Active Directory? The samples I have seen so far don’t get the User Address, Email, or Phone Number! Thanks.
 
This is an exmple from a previous post. Take a look at the other posts,
there are a lot of questions about system.directoryservices. If you have
more specific questions post them and we can help further.
Jared

Sub GetInfo(ByVal UserName As String) 'User name - NOT display name

Dim Entry As New DirectoryEntry("LDAP://RootDSE")
Dim DomainPath As String = "LDAP://" &
Entry.Properties("defaultNamingContext").Value
Dim Results As SearchResultCollection, Result As SearchResult
'Domain Naming Context
'DC=MyDomain,DC=com
Entry = New DirectoryEntry(DomainPath)
Dim Searcher As New DirectorySearcher(Entry)
With Searcher
.SearchScope = SearchScope.Subtree
.Filter = "(&(objectClass=person)(sAMAccountName=" & UserName &
"))"
'Only works when threaded. Keep this in mind.
'Default size returned is 1000 (server set)
.PageSize = 100
.PropertiesToLoad.AddRange(New String() {"sAMAccountName",
"displayName"})
Result = .FindOne
End With
If Not Result Is Nothing Then
'Watch for multivalued properties
If TypeOf Result.GetDirectoryEntry.Properties("displayName") Is
System.Array Then
MessageBox.Show(Result.GetDirectoryEntry.Properties("displayName")(0).Value)
Else
MessageBox.Show(Result.GetDirectoryEntry.Properties("displayName").Value)
End If
End If
With Searcher
'You can use wild cards, Look up the RFC on LDAP for more info
.Filter = "(&(objectClass=person)(sAMAccountName=" & UserName &
"*))"
Results = Searcher.FindAll
End With
For Each Result In Results
'Same as before
If TypeOf Result.GetDirectoryEntry.Properties("displayName") Is
System.Array Then
Console.WriteLine(Result.GetDirectoryEntry.Properties("displayName")(0).Value)
Else
Console.WriteLine(Result.GetDirectoryEntry.Properties("displayName").Value)
End If
Next
End Sub
 
Back
Top