I have a C # .NET MVC web application on Azure that I have turned Easy Auth for Azure AD accounts. I would like to be able to use the Microsoft Graph SDK with it.
In an Easy Auth article written by cgillum related above, he says:
Ill also indicates that I used HTTP primitives instead of using the official SDK for the Graph API. This was mainly to emphasize that access to the Azure AD Graph API can be written in any language and does not require any SDKs (although you can certainly use the SDKs if you want).
I understand that this was written more than a year ago and taking into account the AAD Graph, but he said that it is possible to use the AAD Graph SDK, so perhaps it is possible for MS Graph. (I would be sincerely glad to see, if anyway, but it seems like a big push for using MS Graph over AAD Graph)
Currently, I can make urls at the Microsoft Graph endpoint ( https://graph.microsoft.com ) in my controller as follows:
public JObject GetMe()
{
string accessToken = this.Request.Headers["X-MS-TOKEN-AAD-ACCESS-TOKEN"];
var url = "https://graph.microsoft.com/v1.0/me";
using (var httpClient = new HttpClient())
{
httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", accessToken);
var response = httpClient.GetStringAsync(url).Result;
var json = JObject.Parse(response);
return json;
}
}
This returns:
{
"@odata.context":"https://graph.microsoft.com/v1.0/$metadata#users/$entity",
"id":"a4cxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx",
"businessPhones":[
"(xxx) xxx-xxxx"
],
"displayName":"Ryan Taite",
"givenName":"Ryan",
"jobTitle":"xxxxxxxxxxxx",
"mail":"xxxxxxx@xxxxxxx.xxxxx",
"mobilePhone":"(xxx) xxx-xxxx",
"officeLocation":"xxxxxxx",
"preferredLanguage":"xxxxxxx",
"surname":"Taite",
"userPrincipalName":"xxxxxxx@xxxxxxx.xxxxx"
}
Am I limited to constructing URLs?
Is it possible to use Microsoft.Graph SDK and Easy Auth so that I can make calls as shown below, where new AzureAuthenticationProvider()will I use Easy Auth accessTokensomehow ?:
public async Task<User> GetMeViaMsGraph()
{
Microsoft.Graph.GraphServiceClient graphServiceClient = new Microsoft.Graph.GraphServiceClient(authenticationProvider:new AzureAuthenticationProvider());
Microsoft.Graph.User user = await graphServiceClient.Me.Request().GetAsync();
return user;
}
msgraph-sdk-dotnet GitHub , AuthenticationProvider, , , , .
</" > Update:
Nan Yu , , , .
:
public User GetMeViaMsGraph()
{
string accessToken = this.Request.Headers["X-MS-TOKEN-AAD-ACCESS-TOKEN"];
GraphServiceClient graphClient = new GraphServiceClient(new AzureAuthenticationProviderTest(accessToken));
User me = graphClient.Me.Request().GetAsync().Result;
return me;
}
public ActionResult Index()
{
return View(GetMeViaMsGraph());
}
async Task<User> await, .
Index.cshtml , , User :
@using Microsoft.Graph;
@model User
<div>
@Model.DisplayName
@Model.GivenName
@Model.SurName
@Model.JobTitle
</div>