Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

ADO.NET with Active Directory?

Status
Not open for further replies.
Apr 3, 2003
180
US
Does anyone out there know if I can use ADO.NET to connect to, and populate a dataset or datatable with information from an Active Directory database. I have done limited reserch on this but have yet to find anything on linking these two technologies. I am most familiar with ADO.NET but am willing to try anything to connect to AD and retrive User and account information.
Thanks for any replies and have a good day.

"I hear and I forget. I see and I remember. I do and I understand."
- Confucius (551 BC - 479)
 
This should probably be posed in ASP.net forum: forum855.

I hope this helps.



Ron Repp

If gray hair is a sign of wisdom, then I'm a genius.
 
Hello,

I have been trying to do some research on this as well, I need to find a solution to the same problem. I have been told it difficult to retrieve information from the Active Directory.

I could never find a solution to this, if you do find something could you please let me know. And I will do the same for you.

Thanks,

Steve

 
I struggled with the same issue a while ago. Here is a class that I pieced together from info gathered throughout the internet. The output is a dataset of employee names. This has worked pretty good for my needs, hope it helps.

Public Class clsActiveDirectory

Private Function CreateEmptyDataset()
Dim ds As New DataSet
Dim dt As DataTable
dt = ds.Tables.Add("tblEmployees")
'add column
dt.Columns.Add("GivenName", GetType(String))
dt.Columns.Add("SN", GetType(String))
dt.Columns.Add("DisplayName", GetType(String))
Return ds

End Function

Private Function GetLDAPPaths() As DataSet

'This function retrieves LDAP Paths from a database.
'It is a one field table. The field name is LDAPpath
'Here is an example of an entry.
'OU=Inventory Control,OU=mylocation, DC=myserver,DC=local

Dim objConn As New myDatabaseConnectionClass
Dim conn As New SqlClient.SqlConnection
Dim strSelect As String = "SELECT * "
Dim strFrom As String = "FROM myLDAPPathsTable"
Dim strSQL As String = strSelect & strFrom

conn = objConn.connBackEndDB
Dim cmd As New SqlClient.SqlCommand(strSQL, conn)

Dim da As New SqlClient.SqlDataAdapter(cmd)
Dim ds As New DataSet

da.Fill(ds, "tblAD")

Return ds

End Function

Public Function GetMembers() As DataSet

Dim dsLDAP As DataSet = GetLDAPPaths()
Dim o(2) As Object
Dim ds As DataSet = CreateEmptyDataset()
Dim entry As New System.DirectoryServices.DirectoryEntry
Dim mySearcher As New System.DirectoryServices.DirectorySearcher
Dim results As System.DirectoryServices.SearchResultCollection
Dim resEnt1 As System.DirectoryServices.SearchResult
Dim r As DataRow
Dim strPath As String
Dim strGivenName As String
Dim strSN As String
Dim strDisplayName As String

Dim oProp As Object

For Each r In dsLDAP.Tables(0).Rows

strPath = r.Item("LDAPpath").ToString

entry.Path = ("LDAP://" & strPath)

'create a directory searcher which we wrap around the
'directory object we want to search
mySearcher.SearchRoot = entry

mySearcher.Filter = String.Format("(cn=*)")
mySearcher.PropertiesToLoad.Add("givenName")
mySearcher.PropertiesToLoad.Add("sn")
mySearcher.PropertiesToLoad.Add("displayName")


'perform the search and get the result collection back
results = mySearcher.FindAll

'now loop through all the objects in the result collection
'get the found directory entry
For Each resEnt1 In results

If resEnt1.Properties.Contains("givenName") Then
For Each oProp In resEnt1.Properties("givenName")
strGivenName = oProp.ToString
o(0) = strGivenName
Next
Else
o(0) = ""
End If

If resEnt1.Properties.Contains("sn") Then
For Each oProp In resEnt1.Properties("sn")
strSN = oProp.ToString
o(1) = strSN
Next
Else
o(1) = ""
End If

If resEnt1.Properties.Contains("displayName") Then
For Each oProp In resEnt1.Properties("displayName")
strDisplayName = oProp.ToString
o(2) = strDisplayName
Next
Else
o(2) = ""
End If

ds.Tables(0).Rows.Add(o)
Next

Next


results.Dispose()
mySearcher.Dispose()
entry.Close()
entry.Dispose()

Return ds

End Function

End Class
 
Steve,
LDAP is most likley the way to go. There is a book out there called "The .NET Developers Guide to Directory Services Programing" by Joe Kaplan and Ryan Dunn (available at Amazon). All the code is in C# but it is accompanied by an awsome web site that converts the code to VB.NET, the site also has a forum where you can interact with about 600 other members in case you get stuck. Another cool thing is the authors will at time be the ones answering your questions. I have not recived the book yet but have tried some code from the web site and it works great.
Good luck,

Octavian

"I hear and I forget. I see and I remember. I do and I understand."
- Confucius (551 BC - 479)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top