Adding a User to AD using JNDI

Hello,

I am trying to add a user to AD using the sample code below. However, I keep getting the following error.:

javax.naming.directory.InvalidAttributeValueException: [LDAP: error code 21 - 00000057: LdapErr: DSID-0C090B38, comment: Error in attribute conversion operation, data 0, vece

// sample codee:

BasicAttributes attrs = new BasicAttributes();

BasicAttribute ocs = new BasicAttribute("objectclass");

ocs.add("top");

ocs.add("person");

ocs.add("organizationalPerson");

ocs.add("user");

attrs.put(ocs);

BasicAttribute gn = new BasicAttribute("givenName", "test1");

attrs.put(gn);

BasicAttribute sn = new BasicAttribute("sn", "");

attrs.put(sn);

BasicAttribute cn = new BasicAttribute("cn", "test1");

attrs.put(cn);

BasicAttribute uac = new BasicAttribute("userAccountControl", "66048");

attrs.put(uac);

BasicAttribute sam = new BasicAttribute("sAMAccountName", "test1");

attrs.put(sam);

BasicAttribute disName = new BasicAttribute("displayName", "test1");

attrs.put(disName);

BasicAttribute userPrincipalName = new BasicAttribute("userPrincipalName", "test1@tfg.thefiengroup.com");

attrs.put(userPrincipalName);

BasicAttribute instanceType = new BasicAttribute("instanceType", "4");

attrs.put(instanceType);

ctxDC.createSubcontext("CN=test1,CN=Users,DC=Diamelle,DC=local", attrs);

ctxDC.close();

Any ideas as to where the problem may lie?

thanks for your help.

[1554 byte] By [shah70a] at [2007-10-3 4:56:48]
# 1

BasicAttribute sn = new BasicAttribute("sn", "");

is the culprit

Adding an attribute with no value, is equivalent to deleting the value from the attribute.

And because the object hasn't been created yet, and consequently does not have a value for that attribute, you can't delete a value for that attribute.

I can't remember whether this behaviour is due to the the LDAP protocol or whether it is due to the AD implementation.

adler_stevena at 2007-7-14 23:02:03 > top of Java-index,Core,Core APIs...
# 2

Thanks. Adding a value for the sn attribute removes that error.

Now I am getting the following:

javax.naming.OperationNotSupportedException: [LDAP: error code 53 - 0000052D: SvcErr: DSID-031A0FC0, problem 5003 (WILL_NOT_PERFORM), data 0

Could it be that my ctxDC.createSubcontext("CN=test1,CN=Users,DC=Diamelle,DC=local", attrs); values are wrong? if so, how do I find the correct values? I am a little new working with ldap.

Thanks again for your help.

shah70a at 2007-7-14 23:02:03 > top of Java-index,Core,Core APIs...
# 3

Most likely you will also need to set the users password (unicodePwd) when the user object is created. Your group policy is probably set to require a password with certain complexity rules and those all have to be followed. I am pretty sure there are examples in this forum that tell you how to encode "unicodePwd" because it is not just a regular String attribute.

tony.thompsona at 2007-7-14 23:02:03 > top of Java-index,Core,Core APIs...
# 4

Thanks Tony.

I used the following code to create the password string, but I get the same error. Does this need to be over ssl? If so, are the any suggestions on creating and installing the cert for AD?

public static String encodePassword(String pass) throws UnsupportedEncodingException

{

sun.misc.BASE64Encoder encoder = new sun.misc.BASE64Encoder();

final String ATT_ENCODING = "Unicode";

// Agree with MS's ATTRIBUTE_CONSTRAINT

String pwd = "\"" + pass +"\"";

byte _bytes[] = pwd.getBytes(ATT_ENCODING);

// strip unicode marker

byte bytes[] = new byte [_bytes.length - 2];

System.arraycopy(_bytes, 2, bytes, 0,_bytes.length - 2);

String base64 = encoder.encode(bytes);

System.out.println(base64);

return base64;

}

Thanks for your help

shah70a at 2007-7-14 23:02:03 > top of Java-index,Core,Core APIs...
# 5

Most likely it will have to be over SSL. They won't let you do any password things otherwise. The quickest way to setup SSL is to get the CA root cert from your certificate server and import it into your cacerts file in your JVM. If you want to get fancy, you can write your own socket factory that handles the cert when you try to connect (or just ignore the cert and blindly trust it).

tony.thompsona at 2007-7-14 23:02:03 > top of Java-index,Core,Core APIs...
# 6

Have a look at JNDI, Active Directory (Creating new users & demystifying userAccountControl)

http://forum.java.sun.com/thread.jspa?threadID=582103&tstart=15

and

JNDI, Active Directory & Changing Passwords

http://forum.java.sun.com/thread.jspa?threadID=592611&tstart=50

Should answer most of your questions.

adler_stevena at 2007-7-14 23:02:03 > top of Java-index,Core,Core APIs...