1

我需要能够通过使用 LDAP 服务器查询活动目录来获得活动目录中定义的活动用户的列表。

我试图通过成功连接到我的 ldap 服务器来做到这一点。在下面的 java 代码中,使用 accountExpires 属性时,我只返回 1 条记录。我应该从 ldap 服务器取回一个记录列表,其中每条记录都显示 DISPLAY NAME 和 MAIL 属性。

这是我的代码:

public static void main(String[] args) {
    ADUserAttributes adUserAttributes = new ADUserAttributes();
    adUserAttributes.getLdapContext());
    adUserAttributes.getActiveEmpRecordsList("0", adUserAttributes.getLdapContext());
}

public LdapContext getLdapContext(){
    LdapContext ctx = null;
    try{
        Hashtable env = new Hashtable();
        env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
        env.put(Context.SECURITY_AUTHENTICATION, "Simple");
        env.put(Context.SECURITY_PRINCIPAL, "e~inventory"); 
        env.put(Context.SECURITY_CREDENTIALS, "password");
        env.put(Context.PROVIDER_URL, "ldap://xxxdc01.txh.org");
        ctx = new InitialLdapContext(env, null);
        System.out.println("Connection Successful.");
    } catch(NamingException nex){
        System.out.println("LDAP Connection: FAILED");
        nex.printStackTrace();
    }
    return ctx;
}

private List<String> getActiveEmpRecordsList(String accountExpires, LdapContext ctx) {
List<String> activeEmpAttributes = new ArrayList<String>();
Attributes attrs = null;
try {
    SearchControls constraints = new SearchControls();
    constraints.setSearchScope(SearchControls.SUBTREE_SCOPE);
    String[] attrIDs = {"displayname", "mail"};
    constraints.setReturningAttributes(attrIDs);
    NamingEnumeration answer = ctx.search("DC=txh,DC=org", "accountExpires=" + accountExpires, constraints);
    if (answer.hasMore()) {
        attrs = ((SearchResult) answer.next()).getAttributes();
        int empNameLen = attrs.get("displayname").toString().length();
        int empEmailAddrLen = attrs.get("mail").toString().length();
        activeEmpAttributes.add(attrs.get("displayname").toString().substring(13, empNameLen));
        activeEmpAttributes.add(attrs.get("mail").toString().substring(6, empEmailAddrLen));
        ctx.close();
    }else{
        throw new Exception("Invalid User");
    }
    System.out.println("activeEmpAttributes: " + activeEmpAttributes);
    System.out.println("count: " + activeEmpAttributes.size());
} catch (Exception ex) {
    ex.printStackTrace();
}
return activeEmpAttributes;
 }
4

1 回答 1

0

您可以使用 aPrincipalSearcher和“按示例查询”主体进行搜索:

// create your domain context
PrincipalContext ctx = new PrincipalContext(ContextType.Domain);

// define a "query-by-example" principal - here, we search for active UserPrincipals
UserPrincipal qbeUser = new UserPrincipal(ctx);
qbeUser.Enabled = true;

// create your principal searcher passing in the QBE principal    
PrincipalSearcher srch = new PrincipalSearcher(qbeUser);

// find all matches
foreach(var found in srch.FindAll())
{
    // do whatever here - "found" is of type "Principal" - it could be user, group, computer.....          
}

如果您还没有 - 绝对阅读 MSDN 文章Managing Directory Security Principals in the .NET Framework 3.5,它很好地展示了如何充分利用System.DirectoryServices.AccountManagement

您可以在 上指定任何属性UserPrincipal并将其用作PrincipalSearcher.

于 2011-10-17T17:24:17.807 回答