I've been asked to create a simple .Net page that queries our Active
Directory for a lastname. Below is the code I've written. I get an "Unknown
Error" at the
objSearchResultsCollection = objDirectorySearcher.FindAll()
line. I'm not very familiar with Active Directory, so I'm pretty sure that
my connection string or query string is wrong, but I don't even know where to
start to debug this. Do you see anything wrong anywhere? Are there any
tools out there that will help me test my query directly with Active
Directory, or navigate the Active Directory tree? Or do you think the
problem lies not with the connection or query string, but elsewhere? Any
suggestions are welcome. Thanks!
Code:
<%@ Page Language="VB" Debug="true" CodeFile="Default.aspx.vb"
Inherits="_Default" %>
<%@ Import Namespace="System.DirectoryServices" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"
http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="
http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
<script runat="server">
Sub Page_Load(ByVal Source As Object, ByVal E As EventArgs)
'Connect
Dim strLDAPPath As String = ""
strLDAPPath = "ldap://nydc004"
Dim objSearchRoot As New DirectoryEntry(strLDAPPath)
Dim objDirectorySearcher As New DirectorySearcher(objSearchRoot)
'If search has been requested
If IsPostBack = True Then
'Restrict scope of search
objDirectorySearcher.PropertiesToLoad.Add("givenName")
objDirectorySearcher.PropertyNamesOnly = True
'Get info from search box and search
objDirectorySearcher.Filter = GetFilterString()
objDirectorySearcher.Filter = "(dc=kramerlevin,dc=com,
givenName=pete*);givenName;subtree"
objDirectorySearcher.Sort = New SortOption("givenName",
DirectoryServices.SortDirection.Ascending)
Dim objSearchResultsCollection As SearchResultCollection
objSearchResultsCollection = objDirectorySearcher.FindAll()
If Not (objSearchResultsCollection Is Nothing) Then
' Get the DirectoryEntry that corresponds to
objSearchResultsCollection.
Dim objPropColl As ResultPropertyCollection
For Each objSearchResult As SearchResult In
objSearchResultsCollection
objPropColl = objSearchResult.Properties
Message.InnerHtml = objPropColl(1).ToString
For Each strKey As String In objPropColl.PropertyNames
For Each objProp As Object In objPropColl(strKey)
'output results
Next objProp
Next strKey
Next objSearchResult
Message.InnerHtml = "Some hits"
Else
Message.InnerHtml = "No hits"
End If
End If
'Refresh object cache on local computer
objDirectorySearcher.CacheResults = False
End Sub
'Format Query
Public Function FormFilter(ByVal strCategory As String, ByVal strQuery
As String) As String
Dim strResult As String
'strResult = "(dc=kramerlevin,dc=com," & "givenName=" & strQuery &
"*);givenName;subtree"
strResult = "(givenName=" & strQuery & "*)"
Return strResult
End Function
'Get query string
Public Function GetFilterString()
Dim strFilter As String
strFilter = ""
strFilter = FormFilter("user", TextBox1.Text)
Return strFilter
End Function
</script>
</head>
<body>
<form id="form1" runat="server" >
Search Last Name: <asp:TextBox ID="TextBox1"
runat="server"></asp:TextBox>
<asp:Button ID="Button1" runat="server" Text="Search" />
</form>
<div id="Message" runat="server" />
</body>
</html>