File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Security and the fly likes hi Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » Security
Bookmark "hi " Watch "hi " New topic


subin john Malikayil

Joined: Mar 08, 2007
Posts: 7
can any please help me with a hello world user authentication program for with microsoft AD.

i have this code with me, then how do i authenticate a user?

String user = "test";
String password = "test";
String domain = "DC=domain,DC=com"; // if your domain is
Hashtable env = new Hashtable(11);
env.put(Context.PROVIDER_URL, "ldap:///" + domain);
env.put(Context.REFERRAL, "follow");
env.put(Context.SECURITY_PRINCIPAL, "cn=" + user + ",cn=users," + domain);
env.put(Context.SECURITY_CREDENTIALS, password);
env.put(Context.SECURITY_AUTHENTICATION, "simple");
DirContext ctx = null;
ctx = new InitialDirContext(env);
} catch (NamingException e)
throw new Exception("Cannot connect to Ldap server");
SearchControls constrains = new SearchControls();
constrains.setReturningAttributes(new String[] { "sn", "mail",
"givenName", "sAMAccountName" });
NamingEnumeration results = null;
// find all users
results ="", "(objectclass=user)", constrains);
} catch (NamingException e1)
throw new Exception("An Error Occured during Ldap search");
Bear Bibeault
Author and ninkuma

Joined: Jan 10, 2002
Posts: 63852

Please be sure to take the time to compose descriptive subjects for your posts; read this for more information.

[Asking smart questions] [About Bear] [Books by Bear]
I agree. Here's the link:
subject: hi
It's not a secret anymore!