Azure-Samples / active-directory-dotnet-graphapi-web

A .NET 4.5 MVC web app that demonstrates how to query the Azure AD Graph API using the Azure AD Graph Client Library
82 stars 68 forks source link

ASP.NET Core? #50

Open thomas-christiansen opened 7 years ago

thomas-christiansen commented 7 years ago

Any chance that you can make an example on how to connect and query Graph using .Net Core?

BharatRajMeriyala commented 7 years ago

I am looking for the same. In fact have struggled to get the code up and running on Dotnet Core and given up on the same.

AuthorizationCodeReceived function is totally different and I just couldn't find a way to implement the same example there. Any help on this regard will be grateful

VitorX commented 7 years ago

Refer the code sample here

BharatRajMeriyala commented 7 years ago

I am looking for adding users to ad through dotnet core.. not authentication.

Regards, Bharat Raj Meriyala Sent from my lovely Samsung Galaxy S7.

-------- Original message -------- From: VitorX notifications@github.com Date: 09/02/2017 14:44 (GMT+05:30) To: Azure-Samples/active-directory-dotnet-graphapi-web active-directory-dotnet-graphapi-web@noreply.github.com Cc: BharatRajMeriyala bharatrm@hotmail.com, Comment comment@noreply.github.com Subject: Re: [Azure-Samples/active-directory-dotnet-graphapi-web] ASP.NET Core? (#50)

Refer the code sample herehttps://github.com/Azure-Samples/active-directory-dotnet-webapp-webapi-openidconnect-aspnetcore.git

— You are receiving this because you commented. Reply to this email directly, view it on GitHubhttps://github.com/Azure-Samples/active-directory-dotnet-graphapi-web/issues/50#issuecomment-278586884, or mute the threadhttps://github.com/notifications/unsubscribe-auth/AKaUHH4uPzfBb_9UJq2XwDkDz92gKbQfks5ratjqgaJpZM4Lr9II.

VitorX commented 7 years ago

Here is a simple modifying for that code sample for your reference:

private async Task OnAuthorizationCodeReceived(AuthorizationCodeReceivedContext context)
{
    // Acquire a Token for the Graph API and cache it using ADAL.  In the TodoListController, we'll use the cache to acquire a token to the Todo List API
    string userObjectId = (context.Ticket.Principal.FindFirst("http://schemas.microsoft.com/identity/claims/objectidentifier"))?.Value;
    ClientCredential clientCred = new ClientCredential(ClientId, ClientSecret);
    AuthenticationContext authContext = new AuthenticationContext(Authority, new NaiveSessionCache(userObjectId, context.HttpContext.Session));

    AuthenticationResult authResult = await authContext.AcquireTokenByAuthorizationCodeAsync(
    context.ProtocolMessage.Code, new Uri(context.Properties.Items[OpenIdConnectDefaults.RedirectUriForCodePropertiesKey]), clientCred, "https://graph.microsoft.com");

    var graphserviceClient = new GraphServiceClient(
       new DelegateAuthenticationProvider(
   (requestMessage) =>
   {
       requestMessage.Headers.Authorization = new AuthenticationHeaderValue("bearer", authResult.AccessToken);

       return Task.FromResult(0);
   }));

   var user= await graphserviceClient.Users.Request().AddAsync(new User { AccountEnabled=true, DisplayName="user11", MailNickname= "user11", UserPrincipalName= "user11@xxxx.onmicrosoft.com", PasswordProfile=new PasswordProfile {  ForceChangePasswordNextSignIn=false, Password="password01!"} });

    // Notify the OIDC middleware that we already took care of code redemption.
    context.HandleCodeRedemption();
}

Add the dependency for Microsoft Graph: project.json

{
...
  "dependencies": {
   "Microsoft.Graph": "1.1.0"
}
...
}

And to add users to the Azure Active directory, we need to grant the Directory.ReadWrite permission to the app.

BharatRajMeriyala commented 7 years ago

I tried Implementing the code..

But I am unable to make the NaiveSessionCache Work.

Basically OnAuthorizationCodeReceived never fires. I am using Dotnet Core 1.1..