Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

LdapConnection SearchRequest throwing object does not exist error

I am trying to query an LDAP server to find an LDAP user and import it into my system. However, when I try to do this, the request throws a DirectoryOperationException, saying The object does not exist.

ldapConnection.AuthType = AuthType.Negotiate;

if (ldapDomain.UseEncryption)
{
    ldapConnection.SessionOptions.SecureSocketLayer = true;
    ldapConnection.SessionOptions.StartTransportLayerSecurity(null);
}

var credentials = new NetworkCredential(loginName, password, ldapDomain.Name);
ldapConnection.Bind(credentials);

var filter = String.Format("(&(objectCategory=person)(objectClass=user)(anr={0})(!(UserAccountControl:1.2.840.113556.1.4.803:=2)))", loginName);
var request = new SearchRequest(containerDistinguishedName, filter, SearchScope.Subtree);
var response = ldapConnection.SendRequest(request) as SearchResponse;
var entry = response.Entries[0];

I only have to work against a Microsoft/Windows LDAP server. Right now, containerDistinguishedName is empty, but could be filled in with values to further restrict the search filter.

like image 916
cidthecoatrack Avatar asked Feb 11 '26 08:02

cidthecoatrack


1 Answers

From the looks of this, an empty containerDistinguishedName refers to the Root DSE, but a subtreescoped search should still work, assuming the authorization state of the connection permits trawling the DIT (the Root DSE is only returned in the search result if the search sole was base). Try using the base object that "tops" the DIT for containerDistinguishedName, something like dc=example,dc=com.

see also

  • LDAP: Search best practices
  • LDAP: Programming practices
like image 136
Terry Gardner Avatar answered Feb 15 '26 02:02

Terry Gardner



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!