In the simplest terms Microsoft Graph is the easiest way to call the Microsoft APIs be it Users, Groups, Mail, Calendars, Contacts, Files etc. all from a single endpoint. This was previously known as the Office 365 Unified API. It exposes multiple APIs from Microsoft Cloud Services like Outlook, OneDrive, OneNote etc through a single REST API endpoint (https://graph.microsoft.com). Prior to this, in order to fetch data from each of these services you have to make different endpoint calls to the respective services making it a complex procedure. Using Microsoft Graph, you just have to make a single endpoint call to the cloud services and require a single authentication token.
As per Microsoft, “the Microsoft Graph gives you:
@{
ViewBag.Title = "Home Page";
}
<
div
class
=
"jumbotron"
>
h1
>ASP.NET MVC Tutorial</
p
"lead"
>This sample app uses the Graph to read messages in your inbox.</
><
a
href
"@Url.Action("
SignIn", "Home", null, Request.Url.Scheme)"
"btn btn-primary btn-lg"
>Click here to login</
></
</
add
key
"ida:ClientID"
value
"client id"
/>
"ida:ClientSecret"
"client secret"
public
async Task<ActionResult> SignIn()
{
var authContext =
new
AuthenticationContext(SettingsHelper.AzureADAuthority);
// The url in our app that Azure should redirect to after successful signin
Uri redirectUri =
Uri(Url.Action(
"Authorize"
,
"Home"
null
, Request.Url.Scheme));
// Generate the parameterized URL for Azure signin
Uri authUri = await authContext.GetAuthorizationRequestUrlAsync(SettingsHelper.MicrosoftGraphResource, SettingsHelper.ClientId, redirectUri, UserIdentifier.AnyUser,
);
// Redirect the browser to the Azure signin page
return
Redirect(authUri.ToString());
SettingsHelper
static
string
ClientId
get
ConfigurationManager.AppSettings[
]; }
ClientSecret
AzureADAuthority
"https://login.microsoftonline.com/common"
; }
MicrosoftGraphResource
"https://graph.microsoft.com/"
async Task<ActionResult> Authorize()
authCode = Request.Params[
"code"
];
AuthenticationContext authContext =
// Use client ID and secret to establish app identity
ClientCredential credential =
ClientCredential(SettingsHelper.ClientId, SettingsHelper.ClientSecret);
try
//Get the token
var authResult = await authContext.AcquireTokenByAuthorizationCodeAsync(authCode, redirectUri, credential, SettingsHelper.MicrosoftGraphResource);
// Save the token in the session
Session[
"access_token"
] = authResult.AccessToken;
Redirect(Url.Action(
"Inbox"
catch
(AdalException ex)
Content(
.Format(
"ERROR retrieving token: {0}"
, ex.Message));
async Task<ActionResult> Inbox()
token = (
)Session[
if
(
.IsNullOrEmpty(token))
ViewBag.Message =
"Please Login"
;
"Test"
using
(var client =
HttpClient())
(var request =
HttpRequestMessage(HttpMethod.Get, SettingsHelper.MicrosoftGraphResource +
"v1.0/me/messages"
))
request.Headers.Add(
"Authorization"
"Bearer "
+ token);
"Accept"
"application/json;odata.metadata=minimal"
(var response = client.SendAsync(request).Result)
(response.StatusCode == HttpStatusCode.OK)
var json = JObject.Parse(response.Content.ReadAsStringAsync().Result);
JArray messages = (JArray)json[
"value"
var msgHtml =
"<table><tr><th>Subject</th><th>From</th></tr>"
foreach
(var message
in
messages)
msgHtml +=
"<tr><td>"
+ message[
"subject"
] +
"</td><td>"
"from"
][
"emailAddress"
"name"
"</td></tr>"
"</table>"
ViewBag.JsonResponse = msgHtml;
//return Content(ViewBag.JsonResponse);
View();
"ERROR retrieving messages: {0}"
@model IEnumerable<
MVCAppGraph.Models.DisplayMessage
ViewBag.Title = "Inbox";
h2
>Inbox</
id
"divMessages"
>@Html.Raw(ViewBag.JsonResponse)</