Difference between revisions of "Authenticating against Active Directory"

From MyWiki
Jump to: navigation, search
(Created page with "'''Code to check user credentials'''")
 
Line 1: Line 1:
'''Code to check user credentials'''
+
'''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+".campus.goldsmiths.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";
 +
}}
 +
 
 +
~

Revision as of 15:07, 29 August 2014

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+".campus.goldsmiths.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";

}}

~