I'm having problems using the .NET 2.0, System.DirectoryServices assembly to perform a simple LDAP search.
I'm using the following code to perform the search.
1 public void LDAPSearch()
2 {
3 try
4 {
5 lblOutput.Text = "";
6 txtLdapPath.Text = "LDAP://gds-ldap.usc.edu:6
36/dc=usc,
dc=edu";
7 txtUserDN.Text = "uscrdn=usc.edu.xxxx6tr7,o
u=accounts
,dc=usc,dc
=edu";
8 txtPass.Text = "<unavailable>";
9 txtFilter.Text = "(mail=testing@usc.edu)";
10
11 DirectoryEntry oRoot = new DirectoryEntry( txtLdapPath.Text,
12 txtUserDN.Text, txtPass.Text, AuthenticationTypes.Secure
SocketsLay
er );
13
14 DirectorySearcher oSearcher = new DirectorySearcher( oRoot );
15
16 oSearcher.Filter = txtFilter.Text;
17 oSearcher.SearchScope = SearchScope.Subtree;
18
19 SearchResult oResult = oSearcher.FindOne();
20
21 foreach ( string strPropName in oResult.Properties.Propert
yNames )
22 {
23 foreach ( object oItem in oResult.Properties[strProp
Name] )
24 {
25 lblOutput.Text += strPropName + " = " + Convert.ToString( oItem )+"";
26 }
27 }
28 }
29 catch ( System.Runtime.InteropServ
ices.COMEx
ception ne )
30 {
31 if ( ne.ErrorCode == -2147023570 )
32 lblOutput.Text = "Log in failed!";
33 else
34 lblOutput.Text = "LDAP Error: " + ne.Message + " [" + ne.ErrorCode.ToString() + "]";
35 }
36 catch ( Exception ex )
37 {
38 lblOutput.Text = ex.Message;
39 }
40 }
Line 19 (SearchResult oResult = oSearcher.FindOne();) throws the following exception, which has something do with the search base ("dc=usc,dc=edu").
LDAP Error: There is no such object on the server. [-2147016656]
---
What's strange is that the same credentials and search filter works with the Softerra LDAP Browser Microsoft's LDP.EXE test utility. My understanding is that LDP.EXE uses the same underlying ADSI COM components as the DirectoryServices assembly.
Following is the output from the LDP.EXE program.
==========================
==========
==========
==========
==========
==========
==========
**** CONNECT OUTPUT (Host: gds-ldap.usc.edu, port: 636, using SSL)
ld = ldap_sslinit("gds-ldap.usc
.edu", 636, 1);
Error <0x0> = ldap_set_option(hLdap, LDAP_OPT_PROTOCOL_VERSION,
LDAP_VERSION3);
Error <0x0> = ldap_connect(hLdap, NULL);
Error <0x0> = ldap_get_option(hLdap,LDAP
_OPT_SSL,(
void*)&lv)
;
Host supports SSL, SSL cipher strength = 128 bits
Established connection to gds-ldap.usc.edu.
Retrieving base DSA information...
Result <0>: (null)
Matched DNs:
Getting 1 entries:
>> Dn:
1> objectClass: top;
2> namingContexts: o=usc.edu; dc=usc,dc=edu;
28> supportedExtension: 2.16.840.1.113730.3.5.7; 2.16.840.1.113730.3.5.8; 2.16.840.1.113730.3.5.3; 2.16.840.1.113730.3.5.5; 2.16.840.1.113730.3.5.6; 2.16.840.1.113730.3.5.4; 1.3.6.1.4.1.42.2.27.9.6.1;
1.3.6.1.4.1.42.2.27.9.6.2;
1.3.6.1.4.1.42.2.27.9.6.3;
1.3.6.1.4.1.42.2.27.9.6.4;
1.3.6.1.4.1.42.2.27.9.6.5;
1.3.6.1.4.1.42.2.27.9.6.6;
1.3.6.1.4.1.42.2.27.9.6.7;
1.3.6.1.4.1.42.2.27.9.6.8;
1.3.6.1.4.1.42.2.27.9.6.9;
1.3.6.1.4.1.42.2.27.9.6.11
; 1.3.6.1.4.1.42.2.27.9.6.12
; 1.3.6.1.4.1.42.2.27.9.6.13
; 1.3.6.1.4.1.42.2.27.9.6.14
; 1.3.6.1.4.1.42.2.27.9.6.15
; 1.3.6.1.4.1.42.2.27.9.6.16
; 1.3.6.1.4.1.42.2.27.9.6.17
; 1.3.6.1.4.1.42.2.27.9.6.18
; 1.3.6.1.4.1.42.2.27.9.6.19
; 1.3.6.1.4.1.42.2.27.9.6.21
; 1.3.6.1.4.1.42.2.27.9.6.22
; 1.3.6.1.4.1.1466.20037; 1.3.6.1.4.1.4203.1.11.3;
18> supportedControl: 2.16.840.1.113730.3.4.2; 2.16.840.1.113730.3.4.3; 2.16.840.1.113730.3.4.4; 2.16.840.1.113730.3.4.5; 1.2.840.113556.1.4.473; 2.16.840.1.113730.3.4.9; 2.16.840.1.113730.3.4.16; 2.16.840.1.113730.3.4.15; 2.16.840.1.113730.3.4.17; 2.16.840.1.113730.3.4.19; 1.3.6.1.4.1.42.2.27.9.5.2;
1.3.6.1.4.1.42.2.27.9.5.6;
1.3.6.1.4.1.42.2.27.9.5.8;
2.16.840.1.113730.3.4.14; 1.3.6.1.4.1.1466.29539.12;
2.16.840.1.113730.3.4.12; 2.16.840.1.113730.3.4.18; 2.16.840.1.113730.3.4.13;
3> supportedSASLMechanisms: EXTERNAL; GSSAPI; DIGEST-MD5;
2> supportedLDAPVersion: 2; 3;
1> vendorName: Sun Microsystems, Inc.;
1> vendorVersion: Sun Java(TM) System Directory Server/5.2_Patch_4;
1> dataversion: 020080719075454;
1> netscapemdsuffix: cn=ldap://dc=gds-ldap2,dc=
usc,dc=edu
:389;
-----------
**** BIND OUTPUT (UserDn: "uscrdn=usc.edu.xxxx6tr7,o
u=accounts
,dc=usc,dc
=edu")
res = ldap_simple_bind_s(ld, 'uscrdn=usc.edu.xxxx6tr7,o
u=accounts
,dc=usc,dc
=edu', <unavailable>); // v.3
Authenticated as dn:'uscrdn=usc.edu.xxxx6tr
7,ou=accou
nts,dc=usc
,dc=edu'.
**** SEARCH OUTPUT (Base DN: "dc=usc,dc=edu" Filter:"(mail=testing@usc.
edu)")
ldap_search_s(ld, "dc=usc,dc=edu", 2, "(mail=sclin@usc.edu)", NULL, 0, &msg)
Result <0>: (null)
Matched DNs:
Getting 1 entries:
>> Dn: uscrdn=usc.edu.xxxx4xk6,ou
=people,dc
=usc,dc=ed
u
1> uscUSCID: 792632623;
1> uscEmployeeVerifiedLegalFi
rstName: Joe
1> uscEmployeeVerifiedLegalMi
ddleName: John;
1> uscEmployeeVerifiedLegalLa
stName: Smith;
1> uscEmployeeOfficeTelephone
Number: +1 213 740 0000;
1> postalAddress: University of Southern California$University Park Campus$ABC 123, M/C 9999$Los Angeles, CA 90089-9999;
1> mail: testing@usc.edu;
2> uscEmailAlternateAddress: testing@usc.edu; testing.account@usc.edu;
==========================
==========
==========
==========
==========
==========
==========
====
Again, what's strange is that everything works with Microsoft's LDP.EXE test utility but my code fails.
I've been working on this problem for weeks and I'd really appreciate your help.
Massis