Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

hi

 
subin john Malikayil
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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");
}
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64970
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please be sure to take the time to compose descriptive subjects for your posts; read this for more information.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic