I am a beginner and I try to implement client in Java for Active Directory. So far, I have written the following code:
import java.util.Hashtable;
import javax.naming.Context;
import javax.naming.NamingException;
import javax.naming.directory.Attribute;
import javax.naming.directory.Attributes;
import javax.naming.directory.BasicAttribute;
import javax.naming.directory.BasicAttributes;
import javax.naming.ldap.InitialLdapContext;
import javax.naming.ldap.LdapContext;
public class NewUser {
public static void main(String[] args) {
NewUser user = new NewUser("aaa", "bbb", "ccc", "orgunit");
try {
System.out.print(user.addUser());
} catch (NamingException e) {
e.printStackTrace();
}
}
private static final String DOMAIN_NAME = "whatever";
private static final String DOMAIN_ROOT = "dc=xyz"; // ?
private static final String ADMIN_NAME = "CN=Administrator,CN=Users,DC=xyz,DC=xyz";
private static final String ADMIN_PASS = "xxxxxxx";
private static final String DOMAIN_URL = "ldap://xxx.xxx.xx.xx:389";
private String userName, firstName, lastName, organisationUnit;
private LdapContext context;
public NewUser(String userName, String firstName, String lastName, String organisationUnit) {
this.userName = userName;
this.firstName = firstName;
this.lastName = lastName;
this.organisationUnit = organisationUnit;
Hashtable<String, String> env = new Hashtable<String, String>();
env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
env.put(Context.SECURITY_AUTHENTICATION, "simple");
env.put(Context.SECURITY_PRINCIPAL, ADMIN_NAME);
env.put(Context.SECURITY_CREDENTIALS, ADMIN_PASS);
env.put(Context.PROVIDER_URL, DOMAIN_URL);
try {
this.context = new InitialLdapContext(env, null);
} catch (NamingException e) {
System.err.println("Problem creating object: ");
e.printStackTrace();
}
}
public boolean addUser() throws NamingException {
Attributes container = new BasicAttributes();
Attribute objClasses = new BasicAttribute("objectClass");
objClasses.add("top");
objClasses.add("person");
objClasses.add("organizationalPerson");
objClasses.add("user");
String cnValue = new StringBuffer(firstName).append(" ").append(lastName).toString();
Attribute cn = new BasicAttribute("cn", cnValue);
Attribute sAMAccountName = new BasicAttribute("sAMAccountName", userName);
Attribute principalName = new BasicAttribute("userPrincipalName", userName
+ "@" + DOMAIN_NAME);
Attribute givenName = new BasicAttribute("givenName", firstName);
Attribute sn = new BasicAttribute("sn", lastName);
Attribute uid = new BasicAttribute("uid", userName);
container.put(objClasses);
container.put(sAMAccountName);
container.put(principalName);
container.put(cn);
container.put(sn);
container.put(givenName);
container.put(uid);
try {
context.createSubcontext(getUserDN(cnValue, organisationUnit), container);
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
private static String getUserDN(String aUsername, String aOU) {
return "cn=" + aUsername + ",ou=" + aOU + "," + DOMAIN_ROOT;
}
}
All what I need is to create and add an user.
I have got the following error:
javax.naming.PartialResultException: [LDAP: error code 10 - 0000202B: RefErr: DSID 031007F3, data 0, 1 access points
ref 1: 'xyz'
]; remaining name 'cn=bbb ccc,ou=orgunit,dc=xyz'
You will need to know what the FDN where you want to create the user and that it exists.
I would recommend you obtain one of the LDAP Browsers, ldapwiki.willeke.com/wiki/LDAP%20Browsers, so you can see what your AD looks like from LDAP.
You may also find this helpful: ldapwiki.willeke.com/wiki/Determining%20the%20FDN
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With