How To Read Data From AD LDS Into Java

Just helping out the next person, this code took me all day to cobble together.

Key tricky part was how to connect to AD using an NT security account from Java.


public static String ReadBindingInfoFromAd() throws NamingException
    Hashtable env = new Hashtable();   
    env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");   
    env.put(Context.PROVIDER_URL, "LDAP://MyLdapServer:389");
    env.put(Context.SECURITY_AUTHENTICATION, "DIGEST-MD5"); // Use DIGEST-MD5, it works with Windows.
    env.put(Context.SECURITY_PRINCIPAL, "MyNtDomain\\MyNtUser");  // This is your nt domain and user.
    env.put(Context.SECURITY_CREDENTIALS, "MyPassword");    // Nt user account's password.
    DirContext context = new InitialDirContext(env); 
    SearchControls ctrl = new SearchControls();
    ctrl.setCountLimit(1);  // This is a good idea, limits result size. 
    ctrl.setReturningAttributes(new String [] {"serviceBindingInformation"});
    NamingEnumeration enumeration ="CN=MyServer,CN=MyContainer", "(objectclass=serviceConnectionPoint)", ctrl);
    // My search should only return one value.
    if (enumeration.hasMore())
        SearchResult result = (SearchResult);
        Attributes attribs = result.getAttributes();
        NamingEnumeration values = ((BasicAttribute) attribs.get("serviceBindingInformation")).getAll(); 
        if (values.hasMore())
    return "";  

Same code from C# is not as bad because it leverages current user's context:
public static string ReadBindingInfoFromAd()
    using (DirectoryEntry root = new DirectoryEntry("LDAP://MyLdapServer:389/CN=MyServer,CN=MyContainer"))
        return (string)root.Properties["serviceBindingInformation"].Value;

Skip to main content