var servicePoint = new Uri("https://graph.windows.net");
// e.g. xxx.onmicrosoft.com
var serviceRoot = new Uri(servicePoint, "<your tenant>");
// ClientID and SecretKey are defined when you register application
// with Azure AD
const string clientId = "<clientId>";
const string secretKey = "<secretKey>";
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 Graph API Docs,微软建议过渡到Microsoft Graph,因为Azure AD Graph API正在逐步淘汰。 但是,要通过Azure AD轻松获取照片,现在请使用此URL模板: 第一个月 例如(这是一个假的用户ID): https://graph.windows.net/myorganization/users/abc1d234-01ab-1a23-12ab-abc0d123e456/thumbnailPhoto?api-version=1.6个 下面的代码假设您已经有一个经过身份验证的用户,并带有令牌。这是一个过于简单的例子;你会想要改变返回值来满足你的需要,添加错误检查等等。
const string ThumbUrl = "https://graph.windows.net/myorganization/users/{0}/thumbnailPhoto?api-version=1.6";
// Attempts to retrieve the thumbnail image for the specified user, with fallback.
// Returns: Fully formatted string for supplying as the src attribute value of an img tag.
private string GetUserThumbnail(string userId)
{
string thumbnail = "some base64 encoded fallback image";
string mediaType = "image/jpg"; // whatever your fallback image type is
string requestUrl = string.Format(ThumbUrl, userId);
HttpClient client = new HttpClient();
client.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", GetToken());
HttpResponseMessage response = client.GetAsync(requestUrl).Result;
if (response.IsSuccessStatusCode)
{
// Read the response as a byte array
var responseBody = response.Content.ReadAsByteArrayAsync().GetAwaiter().GetResult();
// The headers will contain information on the image type returned
mediaType = response.Content.Headers.ContentType.MediaType;
// Encode the image string
thumbnail = Convert.ToBase64String(responseBody);
}
return $"data:{mediaType};base64,{thumbnail}";
}
// Factored out for use with other calls which may need the token
private string GetToken()
{
return HttpContext.Current.Session["Token"] == null ? string.Empty : HttpContext.Current.Session["Token"].ToString();
}
3条答案
按热度按时间ffvjumwh1#
您可以使用Azure Active Directory图形客户端获取用户缩略图照片
字符串
Azure AD以二进制格式返回用户的照片,您需要转换为Base64字符串
quhf5bfb2#
不支持通过图形资源管理器获取照片。假设“signedInUser”已经包含了已登录的用户实体,那么这个使用客户端库的代码片段应该对您有用……
字符串
或者,你可以通过REST来实现这一点,它应该看起来像这样:GET https://graph.windows.net/myorganization/users/ /thumbnailPhoto?api-version=1.5希望这能有所帮助,
nr7wwzry3#
根据Azure AD Graph API Docs,微软建议过渡到Microsoft Graph,因为Azure AD Graph API正在逐步淘汰。
但是,要通过Azure AD轻松获取照片,现在请使用此URL模板:
第一个月
例如(这是一个假的用户ID):
https://graph.windows.net/myorganization/users/abc1d234-01ab-1a23-12ab-abc0d123e456/thumbnailPhoto?api-version=1.6
个下面的代码假设您已经有一个经过身份验证的用户,并带有令牌。这是一个过于简单的例子;你会想要改变返回值来满足你的需要,添加错误检查等等。
字符串