Authenticating against Active Directory
From MyWiki
Code to check that user credentials are valid
package myservlets; import java.util.*; import javax.naming.*; import javax.naming.directory.*; public class QueryNewAd { public String checkPass(String SERVER, String USERNAME, String PASS){ String server = ""; String userName = ""; String pass = ""; server = SERVER; userName = USERNAME; pass = PASS; try { Hashtable env = new Hashtable(); env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory"); env.put(Context.PROVIDER_URL, "LDAP://"+server+".camping.goldfinger.ac.uk:389"); //replace with your server URL/IP //only DIGEST-MD5 works with our Windows Active Directory env.put(Context.SECURITY_AUTHENTICATION, "DIGEST-MD5"); //No other SALS worked with me env.put(Context.SECURITY_PRINCIPAL, userName); // specify the username ONLY to let Microsoft Happy env.put(Context.SECURITY_CREDENTIALS, pass); //the password DirContext ctx = new InitialDirContext(env); ctx.close(); } catch(NamingException ne) { return "fail"; } //if no exception, the user is already authenticated. return "success"; }} ~