Active directory 使用id从LDAP active directory获取数据?

Active directory 使用id从LDAP active directory获取数据?,active-directory,jndi,ldap-query,Active Directory,Jndi,Ldap Query,我想使用Id从LDAP Active directory获取一些用户信息。 这是我试图连接并获取的代码 SearchControls ctls = new SearchControls(); ctls.setSearchScope(SearchControls.SUBTREE_SCOPE); NamingEnumeration results = ctx.search("DC=erieinsurance,DC=com", "(&(objectCategory=user)(name{0})

我想使用Id从LDAP Active directory获取一些用户信息。 这是我试图连接并获取的代码

SearchControls ctls = new SearchControls();
ctls.setSearchScope(SearchControls.SUBTREE_SCOPE);
NamingEnumeration results = ctx.search("DC=erieinsurance,DC=com", "(&(objectCategory=user)(name{0}))", 
                    new Object[]{Id}, // filter arguments
                ctls); // search controls
            }
if (results.hasMoreElements()) {

}
它不返回givenname和sn的对应值

上面的过滤器有什么问题吗?
如果您有任何建议,我们将不胜感激。

看起来您正在使用JNDI

这是一个小样本

//Create the initial directory context
LdapContext ctx = new InitialLdapContext(env,null);

//Create the search controls
SearchControls ctls = new SearchControls();

//Specify the attributes to return
String returnedAtts[]={"distinguishedName","CN","sAMAccountName"};
ctls.setReturningAttributes(returnedAtts);

//Specify the search scope
ctls.setSearchScope(SearchControls.SUBTREE_SCOPE);

//specify the LDAP search filter
String searchFilter = "(&(objectClass=user)(sAMAccountName="+ theUserToCheck +"))";

//Specify the Base for the search
String searchBase = "DC=erieinsurance,DC=com";

//initialize counter to total the results
int totalResults = 0;

//Search for objects using the filter
NamingEnumeration answer = ctx.search(searchBase, searchFilter, ctls);

//Loop through the search results
while (answer.hasMoreElements()) {
    SearchResult sr = (SearchResult)answer.next();
    totalResults++;
    System.out.println(totalResults + ". " + sr.getName().toString());
}

感谢您的回复…是的,我们正在使用JNDI。现在已解决,搜索参数Id已映射到不同的参数,现在我已映射到“CN”…因此它正在工作…“UserToCheck”可用于LDAP筛选器注入,例如,如果有人指定UserToCheck=“*”)(otherAttr=otherValue)