处理Microsoft Graph的照片响应

时间:2018-03-05 10:45:47

标签: c# azure-active-directory microsoft-graph

我无法弄清楚此调用实际返回的内容以及如何在HTML中将其显示为img标记。当我使用在线Microsoft Graph Explorer测试环境使用相同的URI时,我得到了预期的图像。

var requestUri =
    "https://graph.microsoft.com/v1.0/barfoo.onmicrosoft.com/users/xxxxx-xxxxxx-xxxxx/photo/$value";

var request =
    new HttpRequestMessage(HttpMethod.Get, requestUri);

var accessToken =
    await _authenticationHelper.GetAccessTokenAsync();

request.Headers.Authorization =
    new AuthenticationHeaderValue("Bearer", accessToken);

var response = await client.SendAsync(request);

2 个答案:

答案 0 :(得分:2)

HttpClient可以Stream为您提供回复。

在ASP.NET Core中,我已经用:

完成了这项工作
[HttpGet]
public async Task<IActionResult> ProfilePhoto()
{
    //removed all the code which you already have
    HttpResponseMessage res = await client.SendAsync(request);

    return File(await res.Content.ReadAsStreamAsync(), "image/jpeg");
}

&#34;经典&#34; ASP.NET MVC应该完全相同,至少可以用IActionResult替换ActionResult

然后,您可以将其链接到图像标记,如:

<img src="/Home/ProfilePhoto"/>

答案 1 :(得分:-1)

获得用户照片回复后,您应该能够按以下方式渲染图像:     src =&#34; data:image / jpeg; base64,{{user.profilePic}}&#34;