0
votes

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

2

2 Answers

0
votes

The error code you got 80090308 can be found in winerror.h - and it says

The token supplied to the function is invalid

Most probably this is caused by the fact that Active Directory expects the password to be UTF-8 encoded byte array, and the Java string is not that.

Try changing your code as this:

envVars.put(Context.SECURITY_CREDENTIALS, "password".getBytes("UTF8"));
1
votes

Be carreful, this problem is not completely identified by the code 80090308. Because the data code give some details about the problem:

  • 525 user not found
  • 52e invalid credentials
  • 530 not permitted to logon at this time
  • 531 not permitted to logon at this workstation
  • 532 password expired
  • 533 account disabled
  • 701 account expired
  • 773 user must reset password
  • 775 user account locked

See this page for more detail: http://www-01.ibm.com/support/docview.wss?uid=swg21290631

Many thanks to IBM Support!