Get LDAP-Attributes from an dirContext in Java with the "sAMAccountName" instead of "CN"

Viewed 2868

I'm using Java to connect to an Active Directory and now I like to the the getAttributes method of an DirContext to find all attributes without doing an search.

I found this example http://www.java2s.com/Code/Java/JNDI-LDAP/howtoretrieveallattributesofanamedobject.htm and it worked fine - but it needs the CN of the object and at this moment I don't have the CN.

This is my code:

Hashtable<String, Object> env = new Hashtable<String, Object>(11);
env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
env.put(Context.PROVIDER_URL, "ldap://192.168.18.106:389/OU=journaldesigner,DC=dataplan,DC=intern");
env.put(Context.SECURITY_PRINCIPAL, "kfriese@dataplan.intern" );
env.put(Context.SECURITY_CREDENTIALS, "fridolin" );
env.put(Context.SECURITY_AUTHENTICATION, "simple" );

DirContext ctx = new InitialDirContext(env);

Attributes answer = ctx.getAttributes("cn=Klaus Friese");

If I replace this line:

Attributes answer = ctx.getAttributes("cn=Klaus Friese");

with this

Attributes answer = ctx.getAttributes("sAMAccountName=kfriese");

I get an error:

javax.naming.NameNotFoundException: [LDAP: error code 32 - 0000208D: NameErr: DSID-0310020A, problem 2001 (NO_OBJECT), data 0, best match of:
'OU=JournalDesigner,DC=dataplan,DC=intern'

I searched now for a while but I didn't find anything about that - can I get the attributes with the sAMAccountName? Do I need to do this with the CN? AD Server is an Microsoft Active Directory.

Thanks Klaus

1 Answers
Related