Handling photo response from Microsoft Graph

可紊 提交于 2019-12-24 18:48:33

问题


I can't figure out what actually is returned by this call and how I can display it in HTML as an img tag. When I use the same URI using the online Microsoft Graph Explorer test environment I get the image as intended.

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);

回答1:


HttpClient can give you the response as a Stream.

In ASP.NET Core, I've done this with:

[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");
}

"Classic" ASP.NET MVC should be pretty identical, you would replace IActionResult with ActionResult at least.

You can then link this to an image tag like:

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



回答2:


Once you have the users photo response you should be able to render the image along the following lines: src="data:image/jpeg;base64,{{ user.profilePic }}"



来源:https://stackoverflow.com/questions/49108644/handling-photo-response-from-microsoft-graph

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!