我想查询Active Directory用户属性,包括自定义属性。我正在收到内置属性,但Attributes
对象中的自定义属性为空。我知道它在那里,它已经设置好了,因为我有相当于在PowerShell程序中工作。LdapContext.search()不返回自定义Active Directory属性
下面是我在做什么:
SearchControls constraints = new SearchControls();
constraints.setSearchScope(SearchControls.SUBTREE_SCOPE);
String[] attrIDs = { "distinguishedName", "mail", "mycustomattribute"};
constraints.setReturningAttributes(attrIDs);
NamingEnumeration<SearchResult> answer = ldapContext.search("DC=mydomain,DC=lan", "sAMAccountName="
+ user, constraints);
if (answer.hasMore())
{
Attributes attrs = answer.next().getAttributes();
System.out.println("distinguishedName "+ attrs.get("distinguishedName"));
System.out.println("mail "+ attrs.get("mail"));
System.out.println("custom "+ attrs.get("mycustomattribute"));
}
这正确输出的专有名称和邮件,但它对于自定义属性输出空。
我已经先用PowerShell测试过以确保属性设置正确。我做了$user = Get-ADUser ...
,然后输出$user.mycustomattribute
并得到了预期值。我在我的Java应用程序中需要这个值。
这里是我的思维过程,到目前为止...
做自定义属性需要以不同的方式访问?我不确定,但谷歌查询并没有提出任何暗示。
难道这与查询没有该数据的域控制器有关吗?当设置LdapContext
对象时,我确实有hashtable.put(Context.REFERRAL, "follow");
作为环境属性,所以我猜测那个规则已经出来了。
我刚刚意识到,如果使用ADSI Edit(浏览数据的MS工具),自定义属性显示为<not set>
,如果我将其作为非域管理员用户运行,但如果将其作为域管理员,它在ADSI编辑中正确显示属性。我以为自己弄明白了,所以我将Java应用程序作为域管理员运行,但它仍然无效(获取其他属性,但不是自定义属性)。