问题描述
我们已将 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设计公司网站源码
织梦dede网页模板下载素材销售下载站平台(带会员中心带筛选)
财税代理公司注册代理记账网站织梦模板(带手机端)
成人高考自考在职研究生教育机构网站源码(带手机端)
高端HTML5响应式企业集团通用类网站织梦模板(自适应手机端)