問題描述
我們已將 Azure AD 設置為應用程序中的身份提供者.我們希望在應用程序中顯示應該來自 Azure AD 的個人資料圖片.
We have set the Azure AD as a identity provider in our application. We want to display profile picture that should come from Azure AD, in the application.
為了測試,我在 Azure AD 中添加了一個 Windows Live Id 帳戶(具有個人資料圖片).然后我們使用 Graph Explorer 進行了嘗試,但沒有成功.
In order to test, I have added one Windows Live Id account (which has a profile picture) in the Azure AD. We then tried it using Graph Explorer, but no luck.
我們如何從 Azure AD 中獲取個人資料圖片?
How do we get the profile picture from Azure AD?
推薦答案
您可以使用 Azure Active Directory Graph Client 獲取用戶縮略圖照片
You can use Azure Active Directory Graph Client to get user thumbnail photo
var servicePoint = new Uri("https://graph.windows.net");
var serviceRoot = new Uri(servicePoint, "<your tenant>"); //e.g. xxx.onmicrosoft.com
const string clientId = "<clientId>";
const string secretKey = "<secretKey>";// ClientID and SecretKey are defined when you register application with Azure AD
var authContext = new AuthenticationContext("https://login.windows.net/<tenant>/oauth2/token");
var credential = new ClientCredential(clientId, secretKey);
ActiveDirectoryClient directoryClient = new ActiveDirectoryClient(serviceRoot, async () =>
{
var result = await authContext.AcquireTokenAsync("https://graph.windows.net/", credential);
return result.AccessToken;
});
var user = await directoryClient.Users.Where(x => x.UserPrincipalName == "<username>").ExecuteSingleAsync();
DataServiceStreamResponse photo = await user.ThumbnailPhoto.DownloadAsync();
using (MemoryStream s = new MemoryStream())
{
photo.Stream.CopyTo(s);
var encodedImage = Convert.ToBase64String(s.ToArray());
}
Azure AD 以二進制格式返回用戶照片,需要轉換為 Base64 字符串
Azure AD returns user's photo in binary format, you need to convert to Base64 string
這篇關于從 Azure Active Directory 獲取個人資料圖片的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!