0
votes

I have a .net core web app which was built using the built in template for using office365 account for authentication. The app is working perfectly. I have the clientID, client Secret, registered the app in azure, have the correct scopes in the appsettings json etc etc. I can log in using my office 365 account. What I cant do though is access the graph api as the logged in user.

All the examples talk about creating
GraphServiceClient graphClient = new GraphServiceClient(authProvider); However none of these examples fit the visual studio template.

How can I access the graph api using the logged in user?

1
Basically trying to get this code to work GraphServiceClient graphClient = new GraphServiceClient(authProvider); //var messages = await GetMessages(graphClient); var users = await graphClient.Users .Request() .GetAsync(); ReceiveEmail(); return View(); - Joe Smith
Have you gone through this document: docs.microsoft.com/en-us/graph/sdks/sdks-overview? What is wrong with GraphServiceClient graphClient = new GraphServiceClient(authProvider)? Are there any error messages? Please add more details. - Allen Wu
Hi, yes, I've been through the overview and installed the sdks, including graph, graph.core and graph.auth. The app runs fine and I can log in using my office 365 account. However, still cant pull back any data from the graph. - Joe Smith

1 Answers

0
votes

Generally speaking, you need to Install the Microsoft Graph .NET SDK first.

Then before you could use GraphServiceClient graphClient = new GraphServiceClient(authProvider), you should Choose a Microsoft Graph authentication provider.

In this case, you want to access the graph api using the logged in user. So you should choose Authorization code provider.

IConfidentialClientApplication confidentialClientApplication = ConfidentialClientApplicationBuilder
    .Create(clientId)
    .WithRedirectUri(redirectUri)
    .WithClientSecret(clientSecret) // or .WithCertificate(certificate)
    .Build();

AuthorizationCodeProvider authProvider = new AuthorizationCodeProvider(confidentialClientApplication, scopes);