我想查詢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應用程序作爲域管理員運行,但它仍然無效(獲取其他屬性,但不是自定義屬性)。