問題描述
我一直在嘗試檢索為我們的 Active Directory 用戶設(shè)置的兩個自定義屬性,但似乎我一直在獲取一個恒定的屬性列表(在 2 個不同的 AD 服務(wù)器上測試)
I've been trying to retrieve two custom properties that are set on our Active Directory users, but it seems like I keep getting a constant list of Properties (tested over 2 different AD servers)
假設(shè)我試圖獲取的屬性是 prop1
和 prop2
,我在下面的代碼中做錯了什么:
Assuming the properties I'm trying to fetch are prop1
and prop2
, What am I doing wrong in the following code:
List<String> nProps = new List<string>();
DirectoryEntry directoryEntry = new DirectoryEntry("WinNT://DOM");
foreach (DirectoryEntry child in directoryEntry.Children)
{
// No filtering; ignore schemes that are not User schemes
if (child.SchemaClassName == "User")
{
foreach (var sVar in child.Properties.PropertyNames)
nProps.Add(sVar.ToString());
break;
}
}
nProps
不包含我的任何自定義屬性(不是 prop1
也不是 prop2
)
nProps
does not contain ANY of my custom properties (not prop1
nor prop2
)
(它確實包含其他屬性,例如 BadPasswordAttempts、用戶名等)
(it does contain other properties, like BadPasswordAttempts, Username, etc)
有什么想法嗎?
推薦答案
你確定你的屬性設(shè)置了嗎?如果未設(shè)置它們,它們將不會被列為屬性.
如果您正在尋找特定的屬性,我建議您使用 目錄搜索器
以下示例獲取給定用戶的公司屬性.請注意,您首先驗證該屬性是否存在,然后提取它.
Are you sure your properties are set? if they are not set, they are not gonna be listed as properties.
If you're looking for specific properties, I'd recommend you to use DirectorySearcher
The following example is getting the company property of a given user. Note that you verify if the property exists first, then you extract it.
DirectoryEntry directoryEntry = new DirectoryEntry("WinNT://DOM");
//Create a searcher on your DirectoryEntry
DirectorySearcher adSearch= new DirectorySearcher(directoryEntry);
adSearch.SearchScope = SearchScope.Subtree; //Look into all subtree during the search
adSearch.Filter = "(&(ObjectClass=user)(sAMAccountName="+ username +"))"; //Filter information, here i'm looking at a user with given username
SearchResult sResul = adSearch.FindOne(); //username is unique, so I want to find only one
if (sResult.Properties.Contains("company")) //Let's say I want the company name (any property here)
{
string companyName = sResult.Properties["company"][0].ToString(); //Get the property info
}
這篇關(guān)于檢索用戶的自定義 Active Directory 屬性的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!