none
How to display userprofile photo for currently logged in user (O365)? RRS feed

  • Question

  • Hello. I would like to get an userprofile photo for currently logged in user? But how on earth I do that? The problem seems to be that I am not able get an accesstoken for graph api "https://graph.microsoft.com". "https://graph.windows.net" does work. This is your standard MVC5 solution with O365 authentication.

    Problematic method. Please observe that AcquireTokenSilentAsync throws an exception: "Failed to acquire token silently. Call method AcquireToken" when graphResourceID  https://graph.microsoft.com is used.

            public static async Task<string> GetTokenForApplication()
            {
                string clientId = ConfigurationManager.AppSettings["ida:ClientId"];
                string appKey = ConfigurationManager.AppSettings["ida:ClientSecret"];
                string aadInstance = ConfigurationManager.AppSettings["ida:AADInstance"];
                string graphResourceID = "https://graph.microsoft.com";//>>works "https://graph.windows.net";
                string signedInUserID = ClaimsPrincipal.Current.FindFirst(System.IdentityModel.Claims.ClaimTypes.NameIdentifier).Value;
                string tenantID = ClaimsPrincipal.Current.FindFirst("http://schemas.microsoft.com/identity/claims/tenantid").Value;
                string userObjectID = ClaimsPrincipal.Current.FindFirst("http://schemas.microsoft.com/identity/claims/objectidentifier").Value;
    
                ClientCredential clientcred = new ClientCredential(clientId, appKey);
                AuthenticationContext authenticationContext = new AuthenticationContext(aadInstance + tenantID, new ADALTokenCache(signedInUserID));
                AuthenticationResult authenticationResult = await authenticationContext.AcquireTokenSilentAsync(graphResourceID, clientcred, new UserIdentifier(userObjectID, UserIdentifierType.UniqueId));
                return authenticationResult.AccessToken;
            }

    Other relevant code: 

    [OutputCache(NoStore = true, Duration = 0, VaryByParam = "*")] public async System.Threading.Tasks.Task<JsonResult> GetUserPhotoAsync() { Stream photo = null; var base64 = string.Empty; try { GraphServiceClient graphClient = SDKHelper.GetAuthenticatedClient(); photo = await graphClient.Me.Photo.Content.Request().GetAsync(); base64 = Convert.ToBase64String(photo.ToByteArray()); } catch(Exception e) { //Oh dear!

    } finally { if(photo!=null) photo.Dispose(); } return Json(new { photo = string.Format("data:image/png;base64, {0}", base64) }, JsonRequestBehavior.AllowGet); }

            public static GraphServiceClient GetAuthenticatedClient()
            {
                GraphServiceClient graphClient = new GraphServiceClient(
                    new DelegateAuthenticationProvider(
                        async (requestMessage) =>
                        {
                            string accessToken = await GetTokenForApplication();
                            requestMessage.Headers.Authorization = new AuthenticationHeaderValue("bearer", accessToken);
                            requestMessage.Headers.Add("Prefer", "outlook.timezone=\"" + TimeZoneInfo.Local.Id + "\"");
                        }));
                return graphClient;
            }


    Sunday, November 11, 2018 1:00 PM