I'm trying to enable a user in the Active Directory with LDAP and Java (1.4). However I keep getting the following error:
java.lang.NullPointerException at com.sun.jndi.ldap.LdapCtx.c_modifyAttributes(LdapCtx.java:1432) at com.sun.jndi.toolkit.ctx.ComponentDirContext.p_modifyAttributes(ComponentDir Context.java:255) at com.sun.jndi.toolkit.ctx.PartialCompositeDirContext.modifyAttributes(Partial CompositeDirContext.java:172) at com.sun.jndi.toolkit.ctx.PartialCompositeDirContext.modifyAttributes(Partial CompositeDirContext.java:161) at javax.naming.directory.InitialDirContext.modifyAttributes(InitialDirContext. java:146) at be.ideal.LDAP.newuser.main(newuser.java:61) Exception in thread "main"
I already confirmed that my user has a password, I just can't seem to change his status to active
My code:
public static void main(String[] args) {
String userName = "cn=Albert Einstein,ou=Accounts,DC=PORTAL,DC=COMPANY,DC=BE";
String groupName = "cn=Administrators,cn=Roles,DC=PORTAL,DC=COMPANY,DC=BE";
boolean isDisabled = false;
try {
System.out.println("Creating initial directory context...");
LdapContext ctx = new InitialLdapContext(X_Ldap.getEnvironment(),
null);
Attributes attrs = new BasicAttributes(true);
attrs.put("objectClass", "user");
attrs.put("cn", "Albert Einstein");
String newQuotedPassword = "\"Pass123\"";
byte[] newUnicodePassword = newQuotedPassword.getBytes("UTF-16LE");
attrs.put(new BasicAttribute("unicodePwd", newUnicodePassword));
attrs.put(new BasicAttribute("msDS-UserAccountDisabled", "FALSE"));
System.out.println("Creating context...");
Context result = ctx.createSubcontext(userName, attrs);
System.out.println("Created account for: " + userName);
System.out.println("Creating context...");
Context result = ctx.createSubcontext(userName, attrs);
System.out.println("Created account for: " + userName);
try {
ModificationItem member[] = new ModificationItem[1];
member[0] = new ModificationItem(DirContext.ADD_ATTRIBUTE,
new BasicAttribute("member", userName));
ctx.modifyAttributes(groupName, member);
System.out.println("Added user to group: " + groupName);
} catch (NamingException e) {
System.err.println("Problem adding user to group: " + e);
}
ctx.close();
System.out.println("Successfully created User: " + userName);
} catch (NamingException e) {
System.err.println("Problem creating object: " + e);
}
catch (IOException e) {
System.err.println("Problem creating object: " + e);
}
}
PS: I am using AD LDS for my active directory
Found it: Needed to use DirContext.ADD_ATTRIBUTE instead of DirContext.REPLACE_ATTRIBUTE
来源:https://stackoverflow.com/questions/9820883/enable-an-active-directory-user-with-ldap-java