c# - Asp.Net Mvc 5 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 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."}}}

最佳答案

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

您可以通过登录新的azure protal来检查是否设置了缩略图,并比较右上角的图像,如下图所示: 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))}

关于c# - Asp.Net Mvc 5 Azure Active Directory 获取用户配置文件图像并将其保存在服务器上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39518324/

相关文章:

c# - 如何强制运行时常量成为编译时常量?

javascript - 如何计算用户在页面上活跃的时间?

javascript - 我的 javascript 有问题,我不想验证已禁用的 <select> 标签

asp.net-mvc - !ClrStack - ASP.NET MVC 应用程序中的调用显示 <NO DATA>

c# - 除了我自己的客户端代码之外,如何防止访问 URL?

c# - 泛型委托(delegate)声明中 'in, out'的含义

c# - 起订量,如何从另一个接口(interface)内部的接口(interface)验证方法

c# - Telerik RadControls,控制当 EnableEmbeddedScripts 为 true 时生成 ScriptResource.axd 文件的位置

c# - 使用两个不同的 sqldatasources 的嵌套 ListView

asp.net - 使用应该是 SSL 的 Html.BeginForm() 创建表单的最佳方法?