Skip to content
This repository was archived by the owner on Jul 31, 2019. It is now read-only.

ASP.NET Core? #50

Open
thomas-christiansen opened this issue Jan 24, 2017 · 5 comments
Open

ASP.NET Core? #50

thomas-christiansen opened this issue Jan 24, 2017 · 5 comments

Comments

@thomas-christiansen
Copy link

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

@BharatRajMeriyala
Copy link

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
Copy link

VitorX commented Feb 9, 2017

Refer the code sample here

@BharatRajMeriyala
Copy link

BharatRajMeriyala commented Feb 9, 2017 via email

@VitorX
Copy link

VitorX commented Feb 10, 2017

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= "[email protected]", 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
Copy link

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..

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants