User With Password Never Expires on MS-Adam

I am able to create users on MS-ADAM Ldap, but the problem is I am not able to set UserAccountControl attribute to "Password NeverExpires".   Due to this I am not able to login with the created userid unless I goto ldap console and choose "Reset Password".   How to write a code snippet that creates users & sets the UserAccountControl to Password Never Expires.

Here is my program for MS-ADAM user creation :

Here is the code to create user with password never expires on MS-ADAM :

import javax.naming.*;
import javax.naming.ldap.LdapContext;
import javax.naming.ldap.InitialLdapContext;
import java.util.Hashtable;
import java.util.Enumeration;
/** * Sample JNDI client ADD application to demonstrate how to create 
a new user entry in MS-ADAM with user account never expires */ public 
class LDAPCreateUser { public static void main(String[] args) throws 
UnsupportedEncodingException { Hashtable env = new Hashtable(); env.put (Context.INITIAL_CONTEXT_FACTORY,"com.sun.jndi.ldap.LdapCtxFactory");
(Context.PROVIDER_URL, "ldap://esnth001:636/dc=ldapbasedn,dc=com");
env.put(Context.SECURITY_AUTHENTICATION, "simple"); env.put(Context.SECURITY_PROTOCOL, "ssl"); env.put (Context.SECURITY_PRINCIPAL, "cn=adminuser,dc=ldapbasedn,dc=com");
env.put(Context.SECURITY_CREDENTIALS, "adminpwd");
try { // Create the initial context // DirContext ctx = new 
LdapContext ctx = new InitialLdapContext(env,null);
// The distinguished name of the new entry String dn = "cn="+args[0]
// Create attributes to be associated with the new entry Attributes 
attrs = new BasicAttributes(true);
// Objectclass -- required in MUST list Attribute oc = new 
// required by 'top' oc.add("top");
// Other mandatory attributes -- required in MUST list attrs.put ("uid", args[0]); attrs.put("sn", "SomeSN"); // required by 'person' attrs.put("givenName","SomeGN"); attrs.put("cn", args[0]); // required by 'person' // Optional attributes -- but they must be 
defined in schema attrs.put("mail","");
//this 2 props are needed for user creation without password 
expiration in MS-ADAM. attrs.put("msDS-UserAccountDisabled", "FALSE"); attrs.put("msDS-UserDontExpirePassword", "TRUE"); attrs.put("ou", "people"); // Create the context Context result = ctx.createSubcontext(dn, 
System.out.println("Created account for: " + dn);
//set password is a ldap modfy operation //and we'll update the 
userAccountControl //enabling the acount and force the user to update 
ther password //the first time they login ModificationItem[] mods = 
new ModificationItem[1];
//Replace the "unicdodePwd" attribute with a new value //Password 
must be both Unicode and a quoted string String newQuotedPassword 
= "\""+args[0]+"\"";
byte[] newUnicodePassword = newQuotedPassword.getBytes("UTF-16LE");
mods[0] = new ModificationItem(DirContext.REPLACE_ATTRIBUTE, new 
BasicAttribute("userPassword", newUnicodePassword));
// Perform the update ctx.modifyAttributes(dn, mods); System.out.println("Set password & updated userccountControl"); // Close the contexts when we're done result.close(); ctx.close(); }catch(NamingException e){ e.printStackTrace(); } } }

*-- SreeNageswara

Have a Java Problems
Do you have a Java Question?

Java Books
Java Certification, Programming, JavaBean and Object Oriented Reference Books

Best Regards,
Java Programming Hints and Tips

All the site contents are Copyright © and the content authors. All rights reserved.
All product names are trademarks of their respective companies.
The site is not affiliated with or endorsed by any company listed at this site.
Every effort is made to ensure the content integrity.  Information used on this site is at your own risk.
 The content on this site may not be reproduced or redistributed without the express written permission of or the content authors.