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 domain.com
Hashtable env = new Hashtable(11);
env.put(Context.INITIAL_CONTEXT_FACTORY,
"com.sun.jndi.ldap.LdapCtxFactory");
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;
try
{
ctx = new InitialDirContext(env);
} catch (NamingException e)
{
throw new Exception("Cannot connect to Ldap server");
}
SearchControls constrains = new SearchControls();
constrains.setSearchScope(SearchControls.SUBTREE_SCOPE);
constrains.setReturningAttributes(new String[] { "sn", "mail",
"givenName", "sAMAccountName" });
NamingEnumeration results = null;
try
{
// find all users
results = ctx.search("", "(objectclass=user)", constrains);
} catch (NamingException e1)
{
throw new Exception("An Error Occured during Ldap search");
}