首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >c#如何使用microsoft graph api获取office 365用户照片

c#如何使用microsoft graph api获取office 365用户照片
EN

Stack Overflow用户
提问于 2017-02-09 09:24:33
回答 4查看 8.4K关注 0票数 7

我希望能够在Azure Active directory中获得所有用户的office365照片。

现在,我可以使用图形SDK获取当前用户的电子邮件

GraphServiceClient graphClient = SDKHelper.GetAuthenticatedClient();

public async Task<string> GetMyEmailAddress(GraphServiceClient graphClient)
    {          
        User me = await graphClient.Me.Request().Select("mail,userPrincipalName").GetAsync();
        return me.Mail ?? me.UserPrincipalName;
    }

但我不确定如何将来自https://graph.microsoft.io/en-us/docs/api-reference/v1.0/api/profilephoto_getthe getting photos part集成到代码中。

感谢任何帮助或代码示例!

EN

回答 4

Stack Overflow用户

发布于 2019-07-09 16:30:25

这有助于获取图像

 GraphServiceClient graphServiceClient = GetAuthenticatedGraphServiceClient();

 Stream photo = await graphServiceClient.Me.Photo.Content.Request().GetAsync();

 if (photo != null)
 {
     MemoryStream ms = new MemoryStream();
     photo.CopyTo(ms);
     byte[] buffer = ms.ToArray();
     string result = Convert.ToBase64String(buffer);
     string imgDataURL = string.Format("data:image/png;base64,{0}", result);
     ViewBag.ImageData = imgDataURL;
 }
 else
 {
      ViewBag.ImageData = "";
 }

票数 7
EN

Stack Overflow用户

发布于 2017-02-09 17:03:31

您可以使用graphClient.Me.Photo.Content获取照片,它将在流中检索照片的二进制数据:

 public async Task GetPictureAsync()
 {
     GraphServiceClient graphClient = GetGraphServiceClient();

     var photo = await graphClient.Me.Photo.Content.Request().GetAsync();
     using (var fileStream = File.Create("C:\\temp\\photo.jpg"))
     {
         photo.Seek(0, SeekOrigin.Begin);
         photo.CopyTo(fileStream);
     }
 }
票数 1
EN

Stack Overflow用户

发布于 2021-10-26 20:15:30

在2021年,你可以这样做:

Stream photo = await graphServiceClient.Me.Photo.Content.Request().GetAsync();
            ViewData["Photo"] = Convert.ToBase64String((photo as MemoryStream).ToArray());

然后在你的html中:

<img src="data:image/png;base64, @ViewData["Photo"]" />
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/42126660

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档