如何使用 Microsoft.Graph SDK 或 User.Identity 获取个人资料图片

问题描述 投票:0回答:2

我已经使用 Microsoft Azure 中的快速入门项目成功实现了身份验证,但现在我无法获取已登录用户的个人资料图片。

我尝试过使用 Microsoft.Graph SDK,但是

Photo
不断变得
null

IConfidentialClientApplication confidentialClientApplication = ConfidentialClientApplicationBuilder
    .Create(ClientId)
    .WithTenantId(TenantId)
    .WithClientSecret(ClientSecret)
    .Build();

AuthorizationCodeProvider authProvider = new AuthorizationCodeProvider(confidentialClientApplication);

// Create an authentication provider.
ClientCredentialProvider authenticationProvider = new ClientCredentialProvider(confidentialClientApplication);
// Configure GraphServiceClient with provider.
GraphServiceClient graphServiceClient = new GraphServiceClient(authenticationProvider);
var users = await graphServiceClient.Users.Request().GetAsync();

我还尝试获取具有 id 和

.Select("Photo")
的特定用户,但结果是相同的

var temp = await graphServiceClient.Users[user_id]
                    .Request()
                    .Select("Photo")
                    .GetAsync();

也许我的实现是错误的?

任何帮助或建议将不胜感激。

c# asp.net-mvc microsoft-graph-api microsoft-graph-sdks
2个回答
1
投票

要以流形式获取照片,您可以使用此代码

using (var photoStream = await graphServiceClient.Users[user_id].Photo.Content
                               .Request()
                               .GetAsync())
{
    // your code       
}

1
投票

当我研究同一问题时,我不断偶然发现这篇文章。我想我应该使用 Graph API 5.6.0 发布我的解决方案。与上面的情况相比,似乎发生了变化。在下面的代码中,

this.Me.Photo
为空,我相信即使它不为空,它也只会包含有关图像的元数据(宽度,高度等)。要获取流,您需要构建 ContentRequestBuilder 并调用 GetAsync 来获取图像流。

我这样做的方法是基于我在应用程序初始化中创建的 GraphServiceClient 服务创建一个 Microsoft.Graph.Me.Photo.Value.ContentRequestBuilder 。有关创建 GraphServiceClient 的详细信息,请参阅在 ASP.NET Core Blazor WebAssembly 中使用 Graph API。

代码:

UserInfo.razor.cs

using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Components; using Microsoft.Graph; using Microsoft.Graph.Models; using System.Security.Cryptography; using PhotoContentRequestBuilder = Microsoft.Graph.Me.Photo.Value.ContentRequestBuilder; namespace GdbRaiser.Pages; [Authorize] public partial class UserInfo { [Inject] public GraphServiceClient? Client { get; set; } private User? Me { get; set; } public string? PhotoBytes { get; set; } protected override async Task OnInitializedAsync() { _ = this.Client?.Me ?? throw new NullReferenceException("Client.Me must not be null"); this.Me = await this.Client.Me.GetAsync(); PhotoContentRequestBuilder photoRequest = new(new Dictionary<string, object>(), this.Client.RequestAdapter); using Stream? photo = await photoRequest.GetAsync(); using StreamReader? reader = photo is null ? null : new StreamReader(new CryptoStream(photo, new ToBase64Transform(), CryptoStreamMode.Read)); this.PhotoBytes = reader is null ? null : await reader.ReadToEndAsync(); } }

更新:经过更多研究后,它变得更加容易。您可以直接使用客户端:

protected override async Task OnInitializedAsync() { _ = this.Client?.Me ?? throw new NullReferenceException("Client.Me must not be null"); this.Me = await this.Client.Me.GetAsync(); using Stream? photo = await this.Client.Me.Photo.Content.GetAsync(); using StreamReader? reader = photo is null ? null : new StreamReader(new CryptoStream(photo, new ToBase64Transform(), CryptoStreamMode.Read)); this.PhotoBytes = reader is null ? null : await reader.ReadToEndAsync(); }
用户信息.razor

@page "/Me" @using System.Reflection; @inject IConfiguration Configuration @code { public static readonly string? Route = typeof(UserInfo).GetCustomAttribute<RouteAttribute>(true)?.Template; public static readonly string Title = "Debug"; public static readonly string[] Icons = { "fa fa-bug-slash" }; } <PageTitle>@(Configuration.GetValue<string>("AppTitle")) - @Title</PageTitle> <h3>Hello @Me?.DisplayName</h3> <table> <tbody> <tr> <td class="text-right"><label class="mr-2">Account Name:</label></td> <td>@(Me?.UserPrincipalName ?? "null")</td> </tr> <tr> <td class="text-right"><label class="mr-2">Phone:</label></td> <td>@(Me?.MobilePhone ?? "null")</td> </tr> <tr> <td class="text-right"><label class="mr-2">Phone:</label></td> <td> @if (PhotoBytes is null) { @("null") } else { <img src="data:image;base64,@PhotoBytes" /> } </td> </tr> </tbody> </table>
    
© www.soinside.com 2019 - 2024. All rights reserved.