从 Azure Active Directory 获取个人资料图片

Get profile picture from Azure Active Directory(从 Azure Active Directory 获取个人资料图片)
本文介绍了从 Azure Active Directory 获取个人资料图片的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

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

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

How to MOQ an Indexed property(如何最小起订量索引属性)
Mocking generic methods in Moq without specifying T(在 Moq 中模拟泛型方法而不指定 T)
How Moles Isolation framework is implemented?(Moles Isolation 框架是如何实现的?)
Difference between Dependency Injection and Mocking Framework (Ninject vs RhinoMocks or Moq)(依赖注入和模拟框架之间的区别(Ninject vs RhinoMocks 或 Moq))
How to mock Controller.User using moq(如何使用 moq 模拟 Controller.User)
How do I mock a class without an interface?(如何模拟没有接口的类?)