问题描述
我正在尝试通过ldap创建一个新的Active Directory用户,但是该用户在创建时被禁用。我试图将userAccountControl属性设置为512,但出现错误WILL_NOT_PERFORM。我读这是因为未设置密码,但我不知道为什么。使用userPassword属性集创建用户的工作正常。
I'm trying to create a new Active Directory user via ldap, but the user is disabled on creation. I am trying to set the userAccountControl attribute to 512, but I am getting an error WILL_NOT_PERFORM. I've read this is because the password isn't being set, but I can't tell why. Creating the user with the userPassword attribute set is working fine.
这是代码:
// Create a container set of attributes
Attributes container = new BasicAttributes();
// Assign the properties we need to set on the user
container.put(new BasicAttribute("objectClass", "user"));
container.put(new BasicAttribute("cn", userName));
container.put(new BasicAttribute("sAMAccountName", userName));
container.put(new BasicAttribute("name", userName));
container.put(new BasicAttribute("givenName", userName));
container.put(new BasicAttribute("userPassword", password));
String fullDomainName = getFullUserName(userName);
// Create the entry
try{
context.createSubcontext(fullDomainName, container);
}catch(Exception e){
System.err.println("Error creating user: " );
e.printStackTrace();
throw e;
}
ModificationItem[] userMods = new ModificationItem[1];
userMods[0] = new ModificationItem(InitialLdapContext.REPLACE_ATTRIBUTE, new BasicAttribute("userAccountControl", "512"));
try{
context.modifyAttributes(fullDomainName, userMods);
}catch(Exception e){
System.err.println("Could not update userAccountControl flag");
e.printStackTrace();
throw e;
}
创建用户的第一部分工作,尝试的第二部分工作设置userAccountControl标志失败。任何帮助将不胜感激。谢谢!
The first part where I create the user works, the 2nd part where I try to set the userAccountControl flag fails. Any help would be greatly appreciated. Thanks!
推荐答案
我发现了问题...我不得不使用unicodePwd属性并确保其正确编码:
I found the problem...I had to use the unicodePwd attribute and make sure it was properly encoded:
final byte[] quotedPasswordBytes = ('"'+password+'"').getBytes("UTF-16LE");
container.put(new BasicAttribute("unicodePwd", quotedPasswordBytes));
我在这里找到了答案:
这篇关于尝试通过LDAP启用用户时出现WILL_NOT_PERFORM错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!