I tried to read the User Names which are in the Active Directory, but I am getting an error. The error is: Exception in thread "main" javax.naming.AuthenticationException: [LDAP: error code 49 - 80090308: LdapErr: DSID-0C0903AA, comment: AcceptSecurityContext error, data 525, v1772]
However, my code is:
package client;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.directory.*;
import javax.naming.ldap.*;
import javax.naming.*;
import java.util.Hashtable;
import java.util.Enumeration;
public class AD {
public AD() {
super();
}
public static void main(String[] args) throws NamingException {
Hashtable envVars = new Hashtable();
envVars.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
envVars.put(Context.PROVIDER_URL, "ldap://" + "IP:Port");
envVars.put(Context.SECURITY_AUTHENTICATION, "simple");
envVars.put(Context.SECURITY_PRINCIPAL, "username");
envVars.put(Context.SECURITY_CREDENTIALS, "password");
DirContext myContext = new InitialDirContext(envVars);
try{
SearchControls searchCtrls = new SearchControls();
searchCtrls.setSearchScope(SearchControls.SUBTREE_SCOPE);
String[] attributes = { "cn", "telephoneNumber", "sn", "userPrincipalName","memberOf","name" };
searchCtrls.setReturningAttributes(attributes);
String filter = "(objectClass=organizationalPerson)";
NamingEnumeration values = myContext.search("CN=Users,DC=bma.gov.bh,DC=gov,DC=bh",filter, searchCtrls);
while (values.hasMoreElements())
{
SearchResult result = (SearchResult) values.next();
Attributes attribs = result.getAttributes();
if (null != attribs)
{
for (NamingEnumeration ae = attribs.getAll(); ae.hasMoreElements();)
{
Attribute atr = (Attribute) ae.next();
String attributeID = atr.getID();
for (
Enumeration vals = atr.getAll();
vals.hasMoreElements();
System.out.println(attributeID +": " +vals.nextElement()) );
}
}
}
myContext.close();
}
catch(Exception e)
{
//e
}
}
}
The problem is starting from this line:
DirContext myContext = new InitialDirContext(envVars);
Can you please help