title: How to set userAccountControl attribute in AD user account
body: <p>I'm creating AD user account using java. I could successfully create the user account and the account did created in the "AD Users and Computers" GUI but I couldn't access the created AD user account.</p>
  
  <p>The problem I encountered is I cannot set the <code>"userAccountControl"</code> attribute to <code>"512"</code> which stands for <code>NORMAL_ACCOUNT</code> or <code>"66048"</code> for <code>NORMAL_ACCOUNT, ACCOUNT_NEVER_EXPIRES</code>.</p>
  
  <p>The following exception is displayed whenever I set the above values:</p>
  
  <pre><code>javax.naming.NameAlreadyBoundException: [LDAP: error code 68 - 00000524: UpdErr: DSID-031A122A, problem 6005 (ENTRY_EXISTS), data 0
  ]; remaining name 'cn=User Four,ou=DAT,DC=dat,DC=com'
  at com.sun.jndi.ldap.LdapCtx.mapErrorCode(Unknown Source)
  at com.sun.jndi.ldap.LdapCtx.processReturnCode(Unknown Source)
  at com.sun.jndi.ldap.LdapCtx.processReturnCode(Unknown Source)
  at com.sun.jndi.ldap.LdapCtx.c_createSubcontext(Unknown Source)
  at com.sun.jndi.toolkit.ctx.ComponentDirContext.p_createSubcontext(Unknown Source)
  at com.sun.jndi.toolkit.ctx.PartialCompositeDirContext.createSubcontext(Unknown Source)
  at com.sun.jndi.toolkit.ctx.PartialCompositeDirContext.createSubcontext(Unknown Source)
  at javax.naming.directory.InitialDirContext.createSubcontext(Unknown Source)
  at NewUser.addUser(NewUser.java:94)
  at MainClass.main(MainClass.java:7)
  </code></pre>
  
  <p>When I create user account directly from AD GUI, the account successfully created with <code>"userAccountControl"</code> attribute <code>"512"</code> or <code>"66048"</code>. And I can access those accounts.</p>
  
  <p>Can anyone tell me how to solve this problem.</p>
  
  <p>Here's my addUser() method.</p>
  
  <pre><code>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);
          Attribute userPassword = new BasicAttribute("userpassword", password);
          Attribute userAccountControl = new BasicAttribute("userAccountControl", "512");
  
          container.put(objClasses);
          container.put(sAMAccountName);
          container.put(principalName);
          container.put(cn);
          container.put(sn);
          container.put(givenName);
          container.put(uid);
          container.put(userPassword);
          container.put(userAccountControl);
  
          try {
              context.createSubcontext(getUserDN(cnValue, organisationUnit), container);
              return true;
          } catch (Exception e) {
              return false;
          }
      }
  </code></pre>