我们已将 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 获取个人资料图片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!