I'm trying to have my custom java application use our Active Directory Server for authentication but I cannot get it to work for some reason. Can anyone see why this is? Here is my method below:

private boolean authenticate(String serverName, String userId, String password) throws NamingException {
    DirContext ctx = null;
    Hashtable env = new Hashtable(11);
    boolean b = false;
    try {
        env.put(Context.INITIAL_CONTEXT_FACTORY,
        "com.sun.jndi.ldap.LdapCtxFactory");
        env.put(Context.PROVIDER_URL, "ldap://servername.org:389");
        env.put(Context.SECURITY_AUTHENTICATION, "simple");
        env.put(Context.SECURITY_PRINCIPAL, "uid="+ userId +",ou=All Users,dc=site,dc=org");
        env.put(Context.SECURITY_CREDENTIALS, password);
        System.out.println("before context");
        // If there isn't a naming exception then the user is authenticated. Return true
        ctx = new InitialDirContext(env);
        //The user is authenticated.
        b = true;
    } catch (NamingException e) {
        System.out.println("the user is not authenticated return false");
        b = false;
    }finally{
        if(ctx != null)
            ctx.close();
    }
    return b;
}

Result:

[12/14/11 16:27:47:746 CST] 0000001f SystemErr     R
javax.naming.AuthenticationException: [LDAP: error code 49 - 80090308: LdapErr: DSID-0C090334, comment: AcceptSecurityContext error, data 52e, vece
share|improve this question
1  
what happens? can you post a stack trace? – Sean Patrick Floyd Dec 14 '11 at 22:05
    
I get an ldap error code 49 which is an authentication error. However, the credentials I'm supplying are correct. I'm able to login to my windows machine and other servers with it. – bschupbach Dec 14 '11 at 22:29
    
does your ldap requires encryption? do you use any specific connection parameters, that you may find important to share? Otherwise it's just guessing. – hovanessyan Dec 14 '11 at 22:42
up vote 4 down vote accepted

Have you tried this way?

//...
env.put(Context.SECURITY_PRINCIPAL, "cn="+ userId +",ou=All Users,dc=site,dc=org");
//...

Also replace

Hashtable env = new Hashtable(11);

with

Hashtable env = new Hashtable();
share|improve this answer
1  
Thanks!! This did the trick...I knew it had to be something simple. – bschupbach Dec 15 '11 at 19:50

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.