Psst! Did you know DevelopmentNow is a mobile web site design agency?

Contact us for help mobilizing your site, or to sign up for our beta Mobile Web SDK!
all groups > dotnet distributed apps > june 2006 >

dotnet distributed apps : Querying LDAP/Active Directory in .Net


Melanie Peterson
6/5/2006 8:52:02 AM
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>
Marc Scheuner
6/5/2006 10:55:52 PM
[quoted text, click to view]

This is definitely an invalid, incomplete LDAP bind string. First of
all, the LDAP part of the bind string needs to be in ALL UPPERCASE -
it *IS* case sensitive.

Also, most likely, your path should (at the very least) look something
like LDAP://dc=yourcompany,dc=com or something like that.

If you don't know what the heck I'm talking about :-), you can either

a) bind to LDAP://RootDSE and have a look at the
"defaultNamingContext" property which should be something like what I
mentioned above, or

b) use any of your favourite ADSI Browsers to find out what your basic
domain name (in LDAP parlance - the "dc" parts of your address, dc
standing for "domain component").

Should you not have any ADSI browsers at hand, may I offer two? ;-)

<shameless plug>
Go see for yourself at
http://adsi.mvps.org/adsi/Delphi/adsibrowser.html
or
http://adsi.mvps.org/adsi/CSharp/beavertail.html

and get either of the two and play around with 'em.
</shameless plug>

HTH
Melanie Peterson
6/6/2006 7:14:01 AM
CASE SENSITIVE!!! Who knew?? It works now. Thank you so much. And I'll
check out your ADSI browsers. :)

[quoted text, click to view]
Melanie Peterson
6/6/2006 10:38:02 AM
Just wanted to add that I tried out your BeaverTail ADSI browser and it's
great! Highly recommended!

[quoted text, click to view]
Marc Scheuner
6/7/2006 12:00:00 AM
[quoted text, click to view]

Thanks, glad to hear it's of help !

AddThis Social Bookmark Button