Asp.net mvc 4 如何在mvc中使用应用程序登录后获取microsoft帐户配置文件照片

Asp.net mvc 4 如何在mvc中使用应用程序登录后获取microsoft帐户配置文件照片,asp.net-mvc-4,azure-active-directory,office365,microsoft-graph-api,Asp.net Mvc 4,Azure Active Directory,Office365,Microsoft Graph Api,在claimprincipal的帮助下,我能够获得如下所示的signedin用户的详细信息,但它没有像谷歌那样提供任何pic相关信息: https://apis.live.net/v5.0/{USER_ID}/picture?type=large 表示URL包含路径“{user\u id}”,该路径不受支持。 甚至试过 https://graph.microsoft.com/v1.0/me/photo/$value 请求访问令牌,但我不确定必须传递什么 string userName = Cl

claimprincipal
的帮助下,我能够获得如下所示的signedin用户的详细信息,但它没有像谷歌那样提供任何pic相关信息:

https://apis.live.net/v5.0/{USER_ID}/picture?type=large
表示URL包含路径
“{user\u id}”
,该路径不受支持。 甚至试过

https://graph.microsoft.com/v1.0/me/photo/$value

请求访问令牌,但我不确定必须传递什么

string userName = ClaimsPrincipal.Current.FindFirst("name").Value;
string userEmail = ClaimsPrincipal.Current.FindFirst(ClaimTypes.Email).Value;
string userId = ClaimsPrincipal.Current.FindFirst(ClaimTypes.NameIdentifier).Value;

想要添加到任何outlook帐户中的图像

您使用live.net API有什么原因吗?而不是Microsoft Graph API?Microsoft Graph API是Microsoft 365消费者和商业帐户中所有用户数据的未来

您可以很容易地获得用户的照片,如这里所述

获取/me/photo/$value

当您使用ASP.NET MVC时,有一个SDK可以让您使用起来非常简单。

用于显示图像。。我们必须使用beared令牌,必须将图像转换为内存流,然后才能使用它。。我用以下方法做了这件事。希望这有助于

 var client = new RestClient("https://login.microsoftonline.com/common/oauth2/token");
                        var request = new RestRequest(Method.POST);
                        request.AddHeader("cache-control", "no-cache");
                        request.AddHeader("content-type", "application/x-www-form-urlencoded");
                        request.AddParameter("application/x-www-form-urlencoded", $"code={code}&client_id={OutClientId}&client_secret={SecretKey}&redirect_uri={OutRedirectUrl}&grant_type=authorization_code", ParameterType.RequestBody);
                        IRestResponse response = client.Execute(request);
                        Token jsonContent = JsonConvert.DeserializeObject<Token>(response.Content);

                        var Token = jsonContent.AccessToken;
                        var TokenType = jsonContent.TokenType;
                        HttpClient httpClient = new HttpClient();
                        httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", Token);
                        HttpResponseMessage response1 = await httpClient.GetAsync("https://graph.microsoft.com/v1.0/me/photos/96x96/$value");
                        if (response1.StatusCode == HttpStatusCode.OK)
                        {
                            using (Stream responseStream = await response1.Content.ReadAsStreamAsync())
                            {
                                MemoryStream ms = new MemoryStream();
                                responseStream.CopyTo(ms);
                                byte[] buffer = ms.ToArray();
                                string result = Convert.ToBase64String(buffer);
                                HttpContext.Session[AppConstants.UserImage] = String.Format("data:image/gif;base64,{0}", result);
                                responseStream.Close();
                            }
                        }
var client=new RestClient(“https://login.microsoftonline.com/common/oauth2/token");
var请求=新的重新请求(Method.POST);
AddHeader(“缓存控制”、“无缓存”);
request.AddHeader(“内容类型”、“应用程序/x-www-form-urlencoded”);
AddParameter(“application/x-www-form-urlencoded”,$“code={code}&client\u id={OutClientId}&client\u secret={SecretKey}&redirect\u uri={OutRedirectUrl}&grant\u type=authorization\u code”,ParameterType.RequestBody);
IRestResponse response=client.Execute(请求);
令牌jsonContent=JsonConvert.DeserializeObject(response.Content);
var Token=jsonContent.AccessToken;
var TokenType=jsonContent.TokenType;
HttpClient HttpClient=新HttpClient();
httpClient.DefaultRequestHeaders.Authorization=新的AuthenticationHeaderValue(“承载者”,令牌);
HttpResponseMessage response1=等待httpClient.GetAsync(“https://graph.microsoft.com/v1.0/me/photos/96x96/$value”);
if(response1.StatusCode==HttpStatusCode.OK)
{
使用(Stream responseStream=await response1.Content.ReadAsStreamAsync())
{
MemoryStream ms=新的MemoryStream();
响应团队副本(ms);
字节[]缓冲区=ms.ToArray();
字符串结果=Convert.tobase64字符串(缓冲区);
会话[AppConstants.UserImage]=String.Format(“数据:image/gif;base64,{0}”,结果);
responseStream.Close();
}
}

通过使用该api,我可以从邮递员那里获取图像,但从代码中,它返回了一些乱七八糟的对象。不确定如何在视图中显示相同的内容。我假设您在这里使用dotnet SDK来执行此操作?二进制对象是base64,因此需要在控件中将其渲染到屏幕上。