2010-04-30 31 views
0

我想通過使用Active Directory搜索用戶的電子郵件。可用的是用戶的全名(例如,「John Doe」用於具有電子郵件「[email protected]」的電子郵件)。從我搜索的內容來看,this接近我所要做的 - 除了過濾器設置爲「SAMAccountName」,這不是我所擁有的。如何在Active Directory中按全名查找電子郵件?

除非我誤解,我只需要選擇正確的屬性並以相同的方式折騰全名。不幸的是,我不知道這個屬性是什麼,顯然沒有人需要以這種方式搜索信息,這是一個非常大的列表(msdn * microsoft * com/en-us/library/ms675090(v = VS.85)* aspx,stackoverflow沒有讓我鏈接2個超鏈接,因爲我沒有10個代表)的屬性。

有誰知道如何使用用戶的全名通過Active Directory查找獲取用戶的電子郵件地址?

回答

3

使用3.5 System.DirectoryServices.AccountManagement.dll,您可以使用UserPrincipal FindByIdentity方法:

UserPrincipal.FindByIdentity(context, IdentityType.Name, "Lastname, Firstname"); 

此調用通常裹一對夫婦使用的語句像下面的內部:

using (var ctx = new PrincipalContext(ContextType.Domain)) 
{  
    using (var userPrincipal = UserPrincipal.FindByIdentity(ctx, IdentityType.Name, user)) 
    { 
     return userPrincipal == null ? "" : userPrincipal.EmailAddress; 
    } 
} 
相關問題