@using System.IO @using Azure.Core @using Microsoft.Graph @inject TeamsFx teamsfx @inject TeamsUserCredential teamsUserCredential

Get the user's profile

@if (NeedConsent) {

Click below to authorize this app to read your profile photo using Microsoft Graph.

Authorize } else if (!string.IsNullOrEmpty(@ErrorMessage)) {
@ErrorMessage
} else if (Profile != null) {
Hello @Profile.DisplayName
}
@code { [Parameter] public string ErrorMessage { get; set; } public bool IsLoading { get; set; } public bool NeedConsent { get; set; } public User Profile { get; set; } private readonly string _scope = "User.Read"; protected override async Task OnInitializedAsync() { IsLoading = true; if (await HasPermission(_scope)) { await ShowProfile(); } } private async Task ShowProfile() { IsLoading = true; var graph = GetGraphServiceClient(); Profile = await graph.Me.Request().GetAsync(); IsLoading = false; ErrorMessage = string.Empty; } private async Task ConsentAndShow() { try { await teamsUserCredential.LoginAsync(_scope); NeedConsent = false; await ShowProfile(); } catch (ExceptionWithCode e) { ErrorMessage = e.Message; } } private async Task HasPermission(string scope) { IsLoading = true; try { await teamsUserCredential.GetTokenAsync(new TokenRequestContext(new string[] { _scope }), new System.Threading.CancellationToken()); return true; } catch (ExceptionWithCode e) { if (e.Code == ExceptionCode.UiRequiredError) { NeedConsent = true; } else { ErrorMessage = e.Message; } } IsLoading = false; return false; } private GraphServiceClient GetGraphServiceClient() { var client = new GraphServiceClient(teamsUserCredential, new string[] { _scope }); return client; } }