0

So I have been driving myself crazy trying to figure out why I can't get my LDAP search to work.

private String getDNFromLDAP(String strUID)
    {
        String strDN = "";

        //Create an LDAP Entry Object
        DirectoryEntry entry = new DirectoryEntry("LDAP://something.blah.com/cn=people,dc=blah,dc=com");
        entry.AuthenticationType = AuthenticationTypes.SecureSocketsLayer;
        entry.Username = "cn=myaccount,cn=special,dc=blah,dc=com";
        entry.Password = "supersecret";

        DirectorySearcher mySearcher = new DirectorySearcher(entry);
        mySearcher.SearchScope = SearchScope.Subtree;
        mySearcher.Filter = "(uid=" + strUID + ")";
        SearchResult result = mySearcher.FindOne();

        int nIndex = result.Path.LastIndexOf("/");
        strDN = result.Path.Substring((nIndex + 1)).ToString().TrimEnd();

        //Clean up objects
        entry.Close();
        entry.Dispose();
        mySearcher.Dispose();

        //returns the DN
        return strDN;
    }

I know the object I am searching for exist (confirmed with ldapsearch), but my result keeps coming back empty. I suspect there is an issue with the base dn, but I don't know how to confirm what what DirectorySearch is using as the base dn. Any help at all would be appreciated.

DR913
  • 119
  • 1
  • 11

1 Answers1

0

You set the root using the searchroot property. The root is set to entry you pass on the constructor, so this might be why you can't find your entry.

Simon Halsey
  • 5,459
  • 1
  • 21
  • 32
  • As I understood it, the DirectorySearcher constructor I am using takes the value of my DirectoryEntry and uses it to set the searchroot property. http://msdn.microsoft.com/en-us/library/y49s2h23.aspx Am I mistaken? – DR913 Jun 21 '13 at 18:00
  • you are correct. My thoughts were since this is the search root, perhaps your search was not able to find the entry you were looking for. You could confirm this by changing the search root to a parent node & seeing how you get on. – Simon Halsey Jun 22 '13 at 23:45
  • After three days of fighting this code, turns out I had a permissions issue. Figured it out by modifying the search root. – DR913 Jun 24 '13 at 14:18