Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Session variables
- String adminUsername = "<administrator sAMAccountName value>";
- String adminPwd = "<admin password>";
- String userDN = "<DN for the user being updated>";
- String newPwd = "<The new password for the user being updated>";
- String ipAddress = "<AD ip address>";
- // LDAP configuration
- String securityProtocol = "sasl";
- String providerURL = "ldap://" + ipAddress;
- Hashtable<Object, Object> env = new Hashtable<>();
- env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
- env.put("javax.security.sasl.strength", "high");
- env.put("javax.security.sasl.policy.noplaintext", "true");
- env.put(Context.PROVIDER_URL, providerURL);
- env.put(Context.SECURITY_AUTHENTICATION, "DIGEST-MD5");
- env.put(Context.SECURITY_PRINCIPAL, adminUsername);
- env.put(Context.SECURITY_CREDENTIALS, adminPwd);
- env.put(Context.SECURITY_PROTOCOL, securityProtocol);
- env.put(Context.REFERRAL, "follow");
- // Prepare the modifications list
- String newQuotedPassword = """ + newPwd + """;
- byte[] newUnicodePassword = newQuotedPassword.getBytes("UTF-16LE");
- ModificationItem[] mods = new ModificationItem[1];
- mods[0] = new ModificationItem(DirContext.REPLACE_ATTRIBUTE,
- new BasicAttribute("unicodePwd", newUnicodePassword));
- // Initiate the LDAP connection
- LdapContext ctx = new InitialLdapContext(env, null);
- // Modify the password
- ctx.modifyAttributes(userDN, mods);
- // Close LDAP connection
- ctx.close();
Add Comment
Please, Sign In to add comment