[英]How to get details of address in Active Directory using UserPrincipal in C#
我正忙於在我的 Windows 服務器上運行的 GUI 應用程序中創建搜索 function 以添加、刪除、更新和搜索用戶。 我幾乎完成了應用程序的構建,但我無法解決從 UserPrincipal 中未提供的其他屬性(如“地址”屬性)獲取詳細信息的問題。 我怎樣才能進入該物業?
我嘗試了許多編碼風格來進入給定的屬性“地址”,但它仍然不起作用。
這是代碼:
private void ListOfUsers(String ou)
{
List<string> users = new List<string>();
PrincipalContext ctx = new PrincipalContext(ContextType.Domain, "EMRE", "OU=" + ou + ",dc=emre,dc=han");
UserPrincipal qbeUser = new UserPrincipal(ctx);
PrincipalSearcher search = new PrincipalSearcher(qbeUser);
foreach (UserPrincipal user in search.FindAll())
{
users.Add(user.UserPrincipalName);
users.Add("********");
users.Add(user.GivenName);
users.Add(user.Surname);
if (user.GetUnderlyingObjectType() == typeof(DirectoryEntry))
{
using (var entry = (DirectoryEntry)user.GetUnderlyingObject())
{
if (entry.Properties["Address"] != null)
users.Add(entry.Properties["Street"].Value.ToString());
}
}
users.Add(user.VoiceTelephoneNumber);
users.Add(user.EmailAddress);
users.Add(ou);
}
string[] row = users.ToArray();
var listViewItem = new ListViewItem(row);
lstStudents.Items.Add(listViewItem);
}
即使屬性不是 null,我也總是返回 null
您想要的屬性實際上稱為streetAddress
。 您還可以使用Properties.Contains
來檢查該值是否存在(盡管效果實際上與檢查null
相同,只是更易於閱讀)。
if (entry.Properties.Contains("streetAddress"))
users.Add(entry.Properties["streetAddress"].Value.ToString());
就個人而言,我喜歡直接使用DirectoryEntry
/ DirectorySearcher
,而不是UserPrincipal
/ PrincipalSearcher
,因為它讓我可以更好地控制它正在做什么,這可以轉化為更好的性能。 我在這里寫了一點: Active Directory:更好的性能
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.