C# Asp.Net Mvc 5 Azure Active Directory在服务器上获取并保存用户配置文件映像

C# Asp.Net Mvc 5 Azure Active Directory在服务器上获取并保存用户配置文件映像,c#,asp.net,asp.net-mvc,azure,azure-active-directory,C#,Asp.net,Asp.net Mvc,Azure,Azure Active Directory,尝试获取已登录用户的配置文件映像,然后将其保存在服务器上 此代码来自Startup.Auth.cs AuthorizationCodeReceived = async (context) => { var code = context.Code; ClientCredential credential = new

尝试获取已登录用户的配置文件映像,然后将其保存在服务器上

此代码来自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();


                           }
wait 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."}}}

根据错误消息,问题的主要可能原因是没有为用户设置缩略图。在为用户设置缩略图之前运行代码时,可以重现此问题。那么代码对我来说很好用

您可以通过登录新的azure protal来检查是否设置了缩略图,并从右上角比较图像,如下图所示:

我从本地Active Directory通过PowerShell进行设置,并通过Azure AD connect将其同步到Azure AD:

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

根据错误消息,问题的主要可能原因是没有为用户设置缩略图。在为用户设置缩略图之前运行代码时,可以重现此问题。那么代码对我来说很好用

您可以通过登录新的azure protal来检查是否设置了缩略图,并从右上角比较图像,如下图所示:

我从本地Active Directory通过PowerShell进行设置,并通过Azure AD connect将其同步到Azure AD:

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