public void SetUserInfo(ApplicationUser account) { var token = account.AccessList.Where(t => Regex.IsMatch(t.Scope, @"user\.read", RegexOptions.IgnoreCase)).FirstOrDefault(); if (token == null) { return; } MSGraphUser user = null; var url = $"https://graph.microsoft.com/v1.0/me/"; using (var httpClient = new HttpClient()) { httpClient.DefaultRequestHeaders.Add("Authorization", "Bearer " + token.AuthTokens.access_token); var res = httpClient.GetAsync(url).Result; string resultJson = res.Content.ReadAsStringAsync().Result; if (res.IsSuccessStatusCode) { user = JsonConvert.DeserializeObject <MSGraphUser>(resultJson); account.businessPhones = user.businessPhones; account.displayName = user.displayName; account.givenName = user.givenName; account.id = user.id; account.jobTitle = user.jobTitle; account.mail = user.mail; account.mobilePhone = user.mobilePhone; account.officeLocation = user.officeLocation; account.preferredLanguage = user.preferredLanguage; account.surname = user.surname; account.userPrincipalName = user.userPrincipalName; } } return; }
public string GetUser(string token) { string result = string.Empty; MSGraphUser user = null; var url = $"https://graph.microsoft.com/v1.0/me/"; using (var httpClient = new HttpClient()) { httpClient.DefaultRequestHeaders.Add("Authorization", "Bearer " + token); var res = httpClient.GetAsync(url).Result; string resultJson = res.Content.ReadAsStringAsync().Result; if (res.IsSuccessStatusCode) { user = JsonConvert.DeserializeObject<MSGraphUser>(resultJson); result = user.displayName; } } return result; }