Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Using c# to search LDAP with Multiple OU's 1

Status
Not open for further replies.

pabowen

Programmer
Nov 6, 2002
95
US
Hello everyone,

I have an application that I am trying to use to search multiple OU's and it fails. It will work fine if I define the OU I want to search, but if I try and search a subtree if fails producing an error of "Unknown error (0x80005000)". I really need help resolving this, as I need to search two separate OU's. The LDAP server is a Sun Java Enterprise Directory Server Version 5.2.

I am really hoping that someone can provide some insight.

Code:
private string getDNFromLDAP(string strUID)
{
	DirectoryEntry entry = new DirectoryEntry("LDAP://ldapserver.orgname.edu/dc=orgname,dc=edu");
	// Works if I use the following entry
	//DirectoryEntry entry = new DirectoryEntry("LDAP://ldapserver.orgname.edu/ou=staff, dc=orgname,dc=edu");
	entry.AuthenticationType = AuthenticationTypes.Encryption;
	entry.Username = this.AdminUser;
	entry.Password = this.AdminPassword;

	DirectorySearcher mySearcher = new DirectorySearcher(entry);
	mySearcher.SearchScope = SearchScope.Subtree;

	mySearcher.Filter = "(&(objectclass=*)(uid=" + strUID + "))";
	try
	{
		SearchResult result = mySearcher.FindOne();
		int nIndex = result.Path.LastIndexOf("/");
				
		string strDN = result.Path.Substring(nIndex + 1).ToString().TrimEnd();
		this.lmsUID = result.Properties[this.xukContainer][0].ToString();
		this.lmsPWD = result.Properties[this.pwdContainer][0].ToString();
		return strDN;
				
	}
	catch(System.Exception ex)
	{
		this.errMsg = ex.Message.toString();
	}
	finally
	{
		mySearcher.Dispose();
		entry.Close();
		entry.Dispose();
	}
}
 
I never recieved any response, so I thought I would post the answer so someone else may be able to use it.

After much work and a very long call to Microsoft I have figured this one out. And it was obscure.

First the error "Unknown error (0x80005000)". Indicates a failure to Bind. This was caused by a unrelated error.

The code was completely ignoring the SearchScope statement. This was due to the AuthenticationType.

Originally the AuthenticationType was set to:
entry.AuthenticationType = AuthenticationTypes.Encryption;

By changing it to:
entry.AuthenticationType = AuthenticationTypes.Encryption | AuthenticationTypes.FastBind;


By including .FastBind the commands started paying attention to the SearchScope. It was unclear what the difference was, but this is what it took to resolve the issue.

Patrick
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top