in C#, I have written below code to connect to LDAP server and query the same.
String ldapUrl = "LDAP://...";
DirectoryEntry entry = new DirectoryEntry(ldapUrl);
DirectorySearcher dSearch = new DirectorySearcher(entry);
String Name = "ravi";
dSearch.Filter = "(&(objectClass=user)(sAMAccountName=" + Name + "))";
foreach (SearchResult sResultSet in dSearch.FindAll())
{
String data = "Login Name :" + (GetProperty(sResultSet, "cn")) + "\r\n" +
"First Name :" + (GetProperty(sResultSet, "givenName")) + "\r\n" +
"Middle Initials :" + (GetProperty(sResultSet, "initials")) + "\r\n" +
"Last Name : " + (GetProperty(sResultSet, "sn"));
}
If you notice, no where I have provided the username and or password. I think it logs-in to the LDAP server using the OS logged in users credentials.
but in JAVA
String url = "ldap://localhost:10389";
Hashtable env = new Hashtable();
env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
env.put(Context.PROVIDER_URL, url);
env.put(Context.SECURITY_AUTHENTICATION, "simple");
env.put(***Context.SECURITY_PRINCIPAL, "uid=admin,ou=system"***);
env.put(***Context.SECURITY_CREDENTIALS, "secret"***);
try {
DirContext ctx = new InitialDirContext(env);
System.out.println("connected");
System.out.println(ctx.getEnvironment());
ctx.close();
} catch (Exception ex) {
System.out.println("error when trying to create the context");
}
Is there a way in java to bind to the LDAP server without providing the username and password? I tried bind to by setting the Context.SECURITY_AUTHENTICATION as NONE, but it them throws the exception for anonymous login not allowed. I don't what to use Anonymous user credentials but the OS logged in users credentials.
is this possible and how?
Regards,