LDAP search to DataGrid from sample code



Could some kind soul please explain to me how i could implement this in a
I found the sample here.
I'm a complete noob so any help is appreciated.


'Sample Code
Public Class ActiveDirectory

'In the following function we will pass the Sam account name to the
function. This can be obtained by reference the Context.User.Indentity.Name
object in .Net. or by referring to the Request.Servervariables("LOGON_USER")
object. The second value passed to the function is the property of the user
class in Active Directory that you want to get a value of.

Public Function GetUserInfo(ByVal inSAM As String, ByVal inType As String)
As String


Dim sPath As String = "LDAP://domain.com/DC=domain,DC=com"

Dim SamAccount As String = Right(inSAM, Len(inSAM) - InStr(inSAM, "\"))

Dim myDirectory As New DirectoryEntry(sPath, "administrator", "password")
'pass the user account and password for your Enterprise admin.

Dim mySearcher As New DirectorySearcher(myDirectory)

Dim mySearchResultColl As SearchResultCollection

Dim mySearchResult As SearchResult

Dim myResultPropColl As ResultPropertyCollection

Dim myResultPropValueColl As ResultPropertyValueCollection

'Build LDAP query

mySearcher.Filter = ("(&(objectClass=user)(samaccountname=" & SamAccount &

mySearchResultColl = mySearcher.FindAll()

'I expect only one user from search result

Select Case mySearchResultColl.Count

Case 0

Return "Null"

Exit Function

Case Is > 1

Return "Null"

Exit Function

End Select

'Get the search result from the collection

mySearchResult = mySearchResultColl.Item(0)

'Get the Properites, they contain the usefull info

myResultPropColl = mySearchResult.Properties

'displayname, mail

'Retrieve from the properties collection the display name and email of the

myResultPropValueColl = myResultPropColl.Item(inType)

Return CStr(myResultPropValueColl.Item(0))

Catch ex As System.Exception

'do some error return here.

End Try

End Function

'Here we will just create some variables and populate them with the call to

'the GetUserInfo function by passing the user account and name of the
property we want to get.

Dim sEmail As String = GetUserInfo(UserAccount, "mail")

Dim sFirstName As String = GetUserInfo(UserAccount, "givenName")

Dim sLastName As String = GetUserInfo(UserAccount, "sn")

Dim sCity As String = GetUserInfo(UserAccount, "l")

Dim sState As String = GetUserInfo(UserAccount, "st")

Dim sStreetAddress As String = GetUserInfo(UserAccount, "streetAddress")

Dim sPostalCode As String = GetUserInfo(UserAccount, "postalCode")

Dim sPhone As String = GetUserInfo(UserAccount, "telephoneNumber")

Dim sCountry As String = GetUserInfo(UserAccount, "co")

End Class


Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question