2016-09-15 35 views
1

嘗試獲取登錄用戶的配置文件映像,然後將其保存在服務器上。Asp.Net Mvc 5 Azure Active Directory在服務器上獲取並保存用戶配置文件圖像

此代碼是從Startup.Auth.cs

AuthorizationCodeReceived = async (context) => 
          { 
           var code = context.Code; 
           ClientCredential credential = new ClientCredential(clientId, appKey); 
           string signedInUserID = context.AuthenticationTicket.Identity.FindFirst(ClaimTypes.NameIdentifier).Value; 
           string userObjectID = context.AuthenticationTicket.Identity.FindFirst("http://schemas.microsoft.com/identity/claims/objectidentifier").Value; 
           AuthenticationContext authContext = new AuthenticationContext(Authority, new ADALTokenCache(signedInUserID)); 
           AuthenticationResult result = await authContext.AcquireTokenByAuthorizationCodeAsync(
           code, new Uri(HttpContext.Current.Request.Url.GetLeftPart(UriPartial.Path)), credential, graphResourceId); 
           AuthenticationResult authenticationResult = await authContext.AcquireTokenSilentAsync(graphResourceId, credential, new UserIdentifier(userObjectID, UserIdentifierType.UniqueId)); 
           Uri servicePointUri = new Uri(graphResourceId); 
           Uri serviceRoot = new Uri(servicePointUri, tenantId); 
           ActiveDirectoryClient activeDirectoryClient = new ActiveDirectoryClient(serviceRoot, async() => await Task.FromResult(result.AccessToken)); 
           var res = await activeDirectoryClient.Users 
           .Where(u => u.ObjectId.Equals(userObjectID)) 
           .ExecuteAsync(); 
           IUser user = res.CurrentPage.ToList().First(); 

           var image = await user.ThumbnailPhoto.DownloadAsync(); 


          } 

等待user.ThumbnailPhoto.DownloadAsync()引發此錯誤

{"odata.error":{"code":"Request_ResourceNotFound","message":{"lang":"en","value":"Resource 'thumbnailPhoto' does not exist or one of its queried reference-property objects are not present."}}} 

回答

0

基於該錯誤消息,主要可能的原因問題在於縮略圖照片沒有爲用戶設置。我在爲用戶設置縮略圖之前運行代碼時,可以重現此問題。然後,代碼適合我。

您可以檢查是否縮略圖通過登錄設置新的蔚藍門脈和圖像從右上角比較喜歡如下圖所示: enter image description here

我將它通過PowerShell與本地Active Directory和同步到Azure的AD Azure AD連接:

Set-ADUser user1 -Replace @{thumbnailPhoto=([byte[]](Get-Content "C:\Users\UserName\Desktop\me.jpg" -Encoding byte))} 
相關問題