Discussions

General J2EE: Active Directory authentication problem

  1. Active Directory authentication problem (2 messages)

    How to use "User logon name" insteads of "Full name" to logon in active directory.

    I would like to use the user logon name combine with password to logon into my application.
    However, I've worked on and I can just logon with full name.

    The following code I worked is success when I logon full name.

    public static int chklogin(String loginName, String password) throws Exception {
    int found = 0;
    Hashtable env = new Hashtable(11);
    env.put(Context.SECURITY_AUTHENTICATION, "simple");
    env.put(Context.SECURITY_PRINCIPAL, "CN=" + loginName + ", CN=users, DC=mydomain");
    env.put(Context.SECURITY_CREDENTIALS, password);
    env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
    env.put(Context.PROVIDER_URL, "ldap://<myip>:389/DC=mydomain");
    env.put(Context.REFERRAL, "follow");
    try {
    DirContext ctx = new InitialDirContext(env);
    String[] sAttrIDs = new String[2];
    Attributes attr = ctx.getAttributes("");
    System.out.println("Domain Name:" + attr.get("name").get());
    found = 1;
    } catch (NamingException e) {
    System.err.println("Problem getting attribute: " + e);
    throw e;
    } catch (Exception e) {
    System.err.println("ERROR: " + e);
    throw e;
    }
    return found;
    }


    How to change it to make it can accept "user logon name"?

    Thanks a lot.

    Please help.

    Thanks

    Regards,

    Kin
  2. Kin -

    Try something like the following:

    Hashtable env = new Hashtable();
    env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
    env.put(Context.PROVIDER_URL, YourLdapUrl);
    env.put(Context.SECURITY_PRINCIPAL, YourDomain\YourLogonName);
    env.put(Context.SECURITY_CREDENTIALS, YourPassword);
    DirContext dir = new InitialDirContext(env);

    - M
  3. I've also been using this one successfully.

    // append the @DOMAIN_NAME onto the user
    env.put(Context.SECURITY_PRINCIPAL, "USERNAME@DOMAIN_NAME");