Difference between revisions of "Authenticating against Active Directory"

From MyWiki
Jump to: navigation, search
 
(One intermediate revision by the same user not shown)
Line 51: Line 51:
  
 
</source>
 
</source>
 
Below is a complete java program to read a text file containing a list of usernames and passwords and then output the success or fail to a text file.
 
~
 

Latest revision as of 12:53, 3 March 2015

Code to check that user credentials are valid

The method "checkPass" is called with three parameters.
The method returns the string "fail" or "success".
The basic code came from a google search.

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";
}}