Ureader.com  
Microsoft software help and Community
   home   |   control panel login   |   archive   |  
 
DotNet
acad.assignment.mngr
academic
adonet
aspnet
aspnet.announcements
aspnet.build.controls
aspnet.caching
aspnet.datagridcontrol
aspnet.mobile
aspnet.security
aspnet.webcontrols
aspnet.webservices
clr
compactframework
component_services
datatools
distributed_apps
drawing
faqs
framework
framework.wmi
general
internationalization
interop
languages.csharp
languages.jscript
languages.vb
languages.vb.controls
languages.vb.data
languages.vb.upgrade
languages.vc
languages.vc.libraries
myservices
odbcnet
performance
remoting
scripting
sdk
security
setup
vjsharp
vsa
webservi.enhancements
webservices
windowsforms
windowsforms.controls
winforms.databinding
winforms.designtime
xml
  
 
date: Mon, 5 Jun 2006 08:52:02 -0700,    group: microsoft.public.dotnet.distributed_apps        back       


Querying LDAP/Active Directory in .Net   
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>
date: Mon, 5 Jun 2006 08:52:02 -0700   author:   Melanie Peterson Melanie

Re: Querying LDAP/Active Directory in .Net   
>Below is the code I've written.  I get an "Unknown Error" at the 
>    objSearchResultsCollection = objDirectorySearcher.FindAll()

>        Dim strLDAPPath As String = ""
>        strLDAPPath = "ldap://nydc004"
>        Dim objSearchRoot As New DirectoryEntry(strLDAPPath)

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
Marc
date: Mon, 05 Jun 2006 22:55:52 +0200   author:   Marc Scheuner

Re: Querying LDAP/Active Directory in .Net   
CASE SENSITIVE!!!  Who knew??  It works now.  Thank you so much.  And I'll 
check out your ADSI browsers.  :)

"Marc Scheuner" wrote:

> >Below is the code I've written.  I get an "Unknown Error" at the 
> >    objSearchResultsCollection = objDirectorySearcher.FindAll()
> 
> >        Dim strLDAPPath As String = ""
> >        strLDAPPath = "ldap://nydc004"
> >        Dim objSearchRoot As New DirectoryEntry(strLDAPPath)
> 
> 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
> Marc
>
date: Tue, 6 Jun 2006 07:14:01 -0700   author:   Melanie Peterson

Re: Querying LDAP/Active Directory in .Net   
Just wanted to add that I tried out your BeaverTail ADSI browser and it's 
great!  Highly recommended!

"Marc Scheuner" wrote:

> >Below is the code I've written.  I get an "Unknown Error" at the 
> >    objSearchResultsCollection = objDirectorySearcher.FindAll()
> 
> >        Dim strLDAPPath As String = ""
> >        strLDAPPath = "ldap://nydc004"
> >        Dim objSearchRoot As New DirectoryEntry(strLDAPPath)
> 
> 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
> Marc
>
date: Tue, 6 Jun 2006 10:38:02 -0700   author:   Melanie Peterson

Re: Querying LDAP/Active Directory in .Net   
>Just wanted to add that I tried out your BeaverTail ADSI browser and it's 
>great!  Highly recommended!

Thanks, glad to hear it's of help !

Marc
date: Wed, 07 Jun 2006 06:51:36 +0200   author:   Marc Scheuner

Google
 
Web ureader.com


    COPYRIGHT 2007, YARDI TECHNOLOGY LIMITED, ALL RIGHT RESERVE  |   contact us