| Author |
Validate is username exists in Active Directory
|
Herman Zweibel
Greenhorn
Joined: Feb 02, 2004
Posts: 12
|
|
Hi, What I want to do is simply validate if a given String username is within ActiveDirectory. I can already authenticate if I have a username and password with the following code: Context ctx = null; Hashtable env = new Hashtable (); boolean isValid = false; try { //set the name of domain with the user name String fullName = userName + "@" + "mycompany.com"; env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap env.put(Context.PROVIDER_URL,"ldap://mycompany.com:389"); env.put(Context.SECURITY_AUTHENTICATION,"simple"); //set user related information env.put(Context.SECURITY_PRINCIPAL, fullName); //set user password env.put(Context.SECURITY_CREDENTIALS, "password"); //validate user ctx = new InitialDirContext(env); isValid = true; System.out.println("IS GOOD isValid is: " + isValid); } catch (AuthenticationException ex) { System.out.println("AuthenticationException is: " + ex); isValid = false; } catch (NamingException ex) { System.out.println("NamingException is: " + ex); } But again, I don't want to authenticate with a username and password - I just want to see if a given username is a valid username withing Active Directory - (I've taken out the env.put(Context.SECURITY_CREDENTIALS, "password"); in the above code and that does not work) so I am sure I need some sort of different approach. Code samples would be greatly appreciated. Many Thanks
|
 |
Lasse Koskela
author
Sheriff
Joined: Jan 23, 2002
Posts: 11962
|
|
1) Log in as an "admin" user (regardless of the username you're trying to validate) 2) Query the directory for the user's username 3) Return true if the query found an entry, false otherwise
|
Author of Test Driven (2007) and Effective Unit Testing (2013) [Blog] [HowToAskQuestionsOnJavaRanch]
|
 |
Herman Zweibel
Greenhorn
Joined: Feb 02, 2004
Posts: 12
|
|
Thanks for the response - How do you query the directory? Any sample code or links would be greatly appreciated. Thanks Again!
|
 |
 |
|
|
subject: Validate is username exists in Active Directory
|
|
|