Guest User

Untitled

a guest
Nov 2nd, 2018
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.39 KB | None | 0 0
  1. // Session variables
  2. String adminUsername = "<administrator sAMAccountName value>";
  3. String adminPwd = "<admin password>";
  4. String userDN = "<DN for the user being updated>";
  5. String newPwd = "<The new password for the user being updated>";
  6. String ipAddress = "<AD ip address>";
  7.  
  8. // LDAP configuration
  9. String securityProtocol = "sasl";
  10. String providerURL = "ldap://" + ipAddress;
  11.  
  12. Hashtable<Object, Object> env = new Hashtable<>();
  13. env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
  14. env.put("javax.security.sasl.strength", "high");
  15. env.put("javax.security.sasl.policy.noplaintext", "true");
  16. env.put(Context.PROVIDER_URL, providerURL);
  17. env.put(Context.SECURITY_AUTHENTICATION, "DIGEST-MD5");
  18. env.put(Context.SECURITY_PRINCIPAL, adminUsername);
  19. env.put(Context.SECURITY_CREDENTIALS, adminPwd);
  20. env.put(Context.SECURITY_PROTOCOL, securityProtocol);
  21. env.put(Context.REFERRAL, "follow");
  22.  
  23. // Prepare the modifications list
  24. String newQuotedPassword = """ + newPwd + """;
  25. byte[] newUnicodePassword = newQuotedPassword.getBytes("UTF-16LE");
  26. ModificationItem[] mods = new ModificationItem[1];
  27. mods[0] = new ModificationItem(DirContext.REPLACE_ATTRIBUTE,
  28. new BasicAttribute("unicodePwd", newUnicodePassword));
  29.  
  30. // Initiate the LDAP connection
  31. LdapContext ctx = new InitialLdapContext(env, null);
  32.  
  33. // Modify the password
  34. ctx.modifyAttributes(userDN, mods);
  35.  
  36. // Close LDAP connection
  37. ctx.close();
Add Comment
Please, Sign In to add comment