#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Posts
    1
    Rep Power
    0

    How to create and add a new user to Active Directory via Java client


    I am a beginner and I try to implement client in Java for Active Directory. So far, I have written the following code:
    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 = "xyz";
        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'
    I hope you will help me.

    Thanks in advance
    Last edited by beginnerLDAPman; April 30th, 2013 at 07:51 AM. Reason: Wrong title

IMN logo majestic logo threadwatch logo seochat tools logo