如何在mvc中使用应用程序登录后获取微软帐户个人资料照片
How to get microsoft account profile photo after login with application in mvc
在 claimprincipal
的帮助下,我能够获得登录用户的详细信息,如下所示,但它没有像 google 那样提供任何图片相关信息:
https://apis.live.net/v5.0/{USER_ID}/picture?type=large
表示 URL 包含不受支持的路径 '{user_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 消费者和商业帐户中所有用户数据的未来。
您可以按照此处所述非常轻松地获取用户照片 https://docs.microsoft.com/en-us/graph/api/profilephoto-get?view=graph-rest-1.0
获取/me/photo/$值
当您使用 ASP.NET MVC 时,您可以使用一个 SDK,这也使这一切变得非常简单。
https://docs.microsoft.com/en-us/graph/sdks/sdks-overview?context=graph%2Fapi%2F1.0&view=graph-rest-1.0
为了显示图像..我们必须使用不记名令牌并将图像转换为内存流然后使用它..我已经通过以下方式完成了。希望这有助于...
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();
}
}
在 claimprincipal
的帮助下,我能够获得登录用户的详细信息,如下所示,但它没有像 google 那样提供任何图片相关信息:
https://apis.live.net/v5.0/{USER_ID}/picture?type=large
表示 URL 包含不受支持的路径 '{user_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 消费者和商业帐户中所有用户数据的未来。
您可以按照此处所述非常轻松地获取用户照片 https://docs.microsoft.com/en-us/graph/api/profilephoto-get?view=graph-rest-1.0
获取/me/photo/$值
当您使用 ASP.NET MVC 时,您可以使用一个 SDK,这也使这一切变得非常简单。 https://docs.microsoft.com/en-us/graph/sdks/sdks-overview?context=graph%2Fapi%2F1.0&view=graph-rest-1.0
为了显示图像..我们必须使用不记名令牌并将图像转换为内存流然后使用它..我已经通过以下方式完成了。希望这有助于...
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();
}
}