您無法從UserPrincipal
課程中獲得用戶的照片 - 您必須進入「更深層次」,例如,獲取底層的DirectoryEntry
對象,然後找出哪個AD屬性持有您的圖片。
using (var ctx = new PrincipalContext(ContextType.Domain,
_webApiServiceConfiguration.Domain,
_webApiServiceConfiguration.DomainAccessUserName,
_webApiServiceConfiguration.DomainAccessPassword))
{
UserPrincipal userAd = UserPrincipal.FindByIdentity(ctx,user.USERID);
// if not null, get the underlying DirectoryEntry
if (userAd != null)
{
DirectoryEntry de = userAd.GetUnderlyingObject() as DirectoryEntry;
// if de is not null
if (de != null)
{
// get the property in question - possibly the "photo" property
if(de.Properties["photo"] != null)
{
// unlike a database column, an AD property can contain *multiple* values....
}
}
}
另外:那些「二進制」屬性通常編碼AD,所以不要指望找到照片就在財產的準確的二進制字節 - 你可能需要做一些轉換或東西... ...
您可能想要搜索「從Active Directory中獲取照片」或其他內容 - 關於如何執行此操作的大量帖子。例如。 this question & answer顯示了一些代碼 - 在DirectoryEntry
對象上似乎也有jpegPhoto
和thumbnailPhoto
屬性....(因此您必須檢查AD中真正填充了哪一個)