Active directory 查询「;主动的;LDAP服务器中的用户不工作

Active directory 查询「;主动的;LDAP服务器中的用户不工作,active-directory,ldap-query,Active Directory,Ldap Query,我需要能够通过使用LDAP服务器查询active directory,从active directory中获取定义的活动用户列表 我已经尝试通过成功连接到ldap服务器来实现这一点。在下面的java代码中,当使用accountExpires属性时,我只返回1条记录。我应该从ldap服务器返回一个记录列表,每个记录都显示显示名称和邮件属性 这是我的密码: public static void main(String[] args) { ADUserAttributes adUserAttr

我需要能够通过使用LDAP服务器查询active directory,从active directory中获取定义的活动用户列表

我已经尝试通过成功连接到ldap服务器来实现这一点。在下面的java代码中,当使用accountExpires属性时,我只返回1条记录。我应该从ldap服务器返回一个记录列表,每个记录都显示显示名称和邮件属性

这是我的密码:

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;
 }
publicstaticvoidmain(字符串[]args){
ADUserAttributes ADUserAttributes=新ADUserAttributes();
getLdapContext());
adUserAttributes.getActiveEmpRecordsList(“0”,adUserAttributes.GetLdaContext());
}
公共LdapContext getLdapContext(){
LdapContext ctx=null;
试一试{
Hashtable env=新的Hashtable();
put(Context.INITIAL\u Context\u工厂,“com.sun.jndi.ldap.LdapCtxFactory”);
环境put(Context.SECURITY_认证,“Simple”);
环境投入(环境安全原则,“e~库存”);
环境put(Context.SECURITY_凭证,“密码”);
env.put(Context.PROVIDER\u URL,“ldap://xxxdc01.txh.org");
ctx=新的InitialLdapContext(env,null);
System.out.println(“连接成功”);
}捕获(NamingException nex){
System.out.println(“LDAP连接:失败”);
nex.printStackTrace();
}
返回ctx;
}
私有列表getActiveEmpRecordsList(字符串accountExpires,LdapContext ctx){
List activeEmpAttributes=new ArrayList();
属性attrs=null;
试一试{
SearchControls约束=新的SearchControls();
约束.setSearchScope(SearchControls.SUBTREE_范围);
字符串[]attrIDs={“displayname”,“mail”};
约束。设置ReturningAttribute(属性ID);
namingumeration 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(“邮件”).toString().length();
add(attrs.get(“displayname”).toString().substring(13,empNameLen));
activeEmpAttributes.add(attrs.get(“mail”).toString().substring(6,empEmailAddrLen));
ctx.close();
}否则{
抛出新异常(“无效用户”);
}
System.out.println(“activeEmpAttributes:+activeEmpAttributes”);
System.out.println(“count:+activeEmpAttributes.size());
}捕获(例外情况除外){
例如printStackTrace();
}
返回activeemp属性;
}

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

// 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文章,这篇文章很好地展示了如何最好地利用
System.DirectoryServices.AccountManagement中的新功能


您可以在
UserPrincipal
上指定任何属性,并将这些属性用作
PrincipalSearcher

Marc的“示例查询”-感谢提供更多信息。我终于能够使我原来的查询工作。感谢您提供MSDN文章的链接。好消息。感谢你的时间。@Melinda:请养成接受最好答案的习惯,即解决你问题的答案。这是在StackOverflow上做的正确和礼貌的事情,并激励其他人继续帮助那些寻找答案的人。