Skip to content

Instantly share code, notes, and snippets.

@ejazhussain
Created July 29, 2020 21:01
Show Gist options
  • Select an option

  • Save ejazhussain/d84ce456f313e7dfa021c3a63c64201d to your computer and use it in GitHub Desktop.

Select an option

Save ejazhussain/d84ce456f313e7dfa021c3a63c64201d to your computer and use it in GitHub Desktop.
ASP.NET Core - Microsoft Graph API Service
public class GraphService : IGraphService
{
private readonly string serviceEndpoint = "https://graph.microsoft.com/v1.0";
public async Task<UserInfo> GetUserProfileAsync(string accessToken)
{
UserInfo profile = new UserInfo();
try
{
HttpClient client = new HttpClient();
client.DefaultRequestHeaders.Add("Authorization", "Bearer " + accessToken);
// Endpoint for my profile
Uri usersEndpoint = new Uri(serviceEndpoint + "/me");
HttpResponseMessage response = await client.GetAsync(usersEndpoint);
if (response.IsSuccessStatusCode)
{
string responseContent = await response.Content.ReadAsStringAsync();
JObject jResult = JObject.Parse(responseContent);
profile.DisplayName = jResult["displayName"].ToString();
profile.JobTitle = jResult["jobTitle"].ToString();
profile.Email = jResult["mail"].ToString();
profile.OfficeLocation = jResult["officeLocation"].ToString();
profile.MobilePhone = jResult["mobilePhone"].ToString();
}
else
{
return null;
}
}
catch (Exception e)
{
return null;
}
return profile;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment