javaldapapache-directory

How to put username with backslash in org.apache.directory.ldap.client.api.LdapConnection?


I have an issue with LDAP connection. When I try to connect with apache directory studio desktop I succesfully autheticate. But when I try to do this with Java it fails. I think that the problem is in username which contains backslash "\".

LdapConnection connection = new LdapNetworkConnection(ldapIp, ldapPort);
connection.bind( "uid=foo\\bar,ou=example,dc=example,dc=com", "mysEcretpa55" );

In this case it say that

Exception in thread "main" org.apache.directory.api.ldap.model.exception.LdapInvalidDnException: unexpected token: b

When I put 4 slashes it say:

LdapErr: DSID-0C0903C8, comment: AcceptSecurityContext error, data 52e, v2580

I read that 52e is an error of bad credentials. I'll repeat again that I verified in apache directory studio desktop application and it works fine.


Solution

  • It's four slashes:

    connection.bind("uid=foo\\\\bar,ou=example,dc=example,dc=com", "mysEcretpa55");
    

    Checkout the documentation in Active Directory: Characters to Escape.

    Two backslashes are for Java String escaping, so you need to double escape, once for Java, and once for LDAP syntax, to get single backslash trough.

    As for the LDAP error you're getting, there are a number of cases when Active Directory rejects a valid password: for instance when authenticating from a client not part of the domain (a Linux box), or if user you're trying to authenticate doesn't have permissions to login to the client computer. But do double-check your password.

    Checkout tips mentioned in this SO answer for alternate ways of authenticating against Windows domain.