Comments are available to members only. Sign up or Log in to view these comments.
Main Topics
Browse All TopicsI'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
7 txtUserDN.Text = "uscrdn=usc.edu.xxxx6tr7,o
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
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
22 {
23 foreach ( object oItem in oResult.Properties[strProp
24 {
25 lblOutput.Text += strPropName + " = " + Convert.ToString( oItem )+"";
26 }
27 }
28 }
29 catch ( System.Runtime.InteropServ
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
Error <0x0> = ldap_set_option(hLdap, LDAP_OPT_PROTOCOL_VERSION,
Error <0x0> = ldap_connect(hLdap, NULL);
Error <0x0> = ldap_get_option(hLdap,LDAP
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;
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;
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=
-----------
**** BIND OUTPUT (UserDn: "uscrdn=usc.edu.xxxx6tr7,o
res = ldap_simple_bind_s(ld, 'uscrdn=usc.edu.xxxx6tr7,o
Authenticated as dn:'uscrdn=usc.edu.xxxx6tr
**** SEARCH OUTPUT (Base DN: "dc=usc,dc=edu" Filter:"(mail=testing@usc.
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
1> uscUSCID: 792632623;
1> uscEmployeeVerifiedLegalFi
1> uscEmployeeVerifiedLegalMi
1> uscEmployeeVerifiedLegalLa
1> uscEmployeeOfficeTelephone
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
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
Business Accounts
Answer for Membership
by: mwvisa1Posted on 2008-09-11 at 16:51:42ID: 22456098
Comments are available to members only. Sign up or Log in to view these comments.