Difference between revisions of "Authenticating against Active Directory"
From MyWiki
(Created page with "'''Code to check user credentials'''") |
|||
(9 intermediate revisions by the same user not shown) | |||
Line 1: | Line 1: | ||
− | '''Code to check user credentials''' | + | '''Code to check that user credentials are valid''' |
+ | |||
+ | The method "checkPass" is called with three parameters.<br> | ||
+ | The method returns the string "fail" or "success".<br> | ||
+ | The basic code came from a google search. | ||
+ | |||
+ | <source lang="java"> | ||
+ | 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"; | ||
+ | }} | ||
+ | |||
+ | </source> |
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"; }}