BIND AD to DataGrid
(Watch for wrapping)
Private Function CheckAD()
Dim objRootDSE = GetObject(ldap://RootDSE/)
Dim adPath As String = "LDAP://" & Mid(objRootDSE.Get("DefaultNamingContext"), InStr(objRootDSE.Get("DefaultNamingContext"), "DC=") + 3, InStr(objRootDSE.Get("DefaultNamingContext"), ",") - 1 - 3)
Dim adAuth As LDAPAuthenticate = New LDAPAuthenticate(adPath)
dgLdap.DataSource = LDAPAuthenticate.LoadADList(adPath)
dgLdap.DataBind()
End Function
The second one actually connects to the Active Directory and returns a list of the searched properties:
Public Shared Function LoadADList(ByVal pathsent As String) As List(Of LDAPAuthenticate)
Dim result As SearchResultCollection = Nothing
Dim entry As DirectoryEntry = New DirectoryEntry(pathsent, "testuser", "password")
Dim obj As Object = entry.NativeObject
Dim search As DirectorySearcher = New DirectorySearcher(entry)
search.Filter = "(&(objectCategory=user)(objectClass=Person)() (sn=*))" ',ObjectClass=user mail=" & sEmailAddress & " (ObjectClass=user)(objectClass=person)
search.PropertiesToLoad.Add("sn")
search.PropertiesToLoad.Add("userAccountControl")
search.PropertiesToLoad.Add("department")
search.PropertiesToLoad.Add("ou")
search.PropertiesToLoad.Add("whenChanged")
search.PropertiesToLoad.Add("employeeType")
search.PropertiesToLoad.Add("userAccountControl")
search.PropertiesToLoad.Add("mail")
search.PropertiesToLoad.Add("telephoneNumber")
search.PropertiesToLoad.Add("samAccountName")
search.PropertiesToLoad.Add("objectGUID")
search.PropertiesToLoad.Add("title")
search.PropertiesToLoad.Add("c")
search.SearchScope = SearchScope.Subtree
Dim resultUsers As SearchResultCollection = search.FindAll()
Dim items As New List(Of LDAPAuthenticate)
For Each srUser As SearchResult In resultUsers
Dim de As DirectoryEntry = srUser.GetDirectoryEntry()
Dim p As New LDAPAuthenticate
Dim strtmp As String = String.Empty
p.USERName = de.Properties("SAMAccountName").Value
p.GivenName = de.Properties("givenName").Value
p.SN = de.Properties("sn").Value
p.MailAcct = de.Properties("mail").Value
p.Department = de.Properties("department").Value
p.Title = de.Properties("title").Value
p.OrgUnit = de.Properties("ou").Value
p.AccountDisabled = de.Properties("userAccountControl").Value
Next
Return items
End Function
Comments