The below code can tell that what we can do with java, .NET guys please don't mind.
String userName = "username";
String passWord = "password";
String base = "cn=Users,dc=ssc,dc=mycompany,dc=com";
Hashtable env = new Hashtable();
public LDAPCrap() {
env.put(Context.INITIAL_CONTEXT_FACTORY,
"com.sun.jndi.ldap.LdapCtxFactory");
env.put(Context.PROVIDER_URL,
"ldap://ssc-dc-01.ssc.mycompany.com:389");
env.put(Context.SECURITY_AUTHENTICATION, "simple");
env.put(Context.SECURITY_PRINCIPAL, "cn=" + userName + "," + base);
env.put(Context.SECURITY_CREDENTIALS, passWord);
}
public void isAuth() {
try {
LdapContext authContext = new InitialLdapContext(env, null);
String filter = "(objectclass=*)";
String sbase = "";
SearchControls controls = new SearchControls();UsernamePasswordHandler handler = new UsernamePasswordHandler(user,
pwd.toCharArray());
LoginContext loginContext = new LoginContext("TEST", handler);
loginContext.login();
// may be a scope change
controls.setSearchScope(SearchControls.SUBTREE_SCOPE);
DirContext ctxGC = new InitialDirContext(env);
NamingEnumeration answer = ctxGC.search(sbase, filter, controls);
SearchResult sr = (SearchResult) answer.next();
javax.naming.directory.Attributes attrs = sr.getAttributes();
System.out.println(" Naming Context: "
+ attrs.get("defaultNamingContext").get());
System.out.println("Bind Successful");
} catch (AuthenticationException authEx) {
System.out.println("Authentication failed!");
} catch (NamingException namEx) {
System.out.println("ERROR IN BIND: " + namEx);
namEx.printStackTrace();
}
}
0 comments:
Post a Comment