Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Validate is username exists in Active Directory

 
Herman Zweibel
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11962
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Herman Zweibel
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the response -
How do you query the directory? Any sample code or links would be greatly appreciated.
Thanks Again!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic