Graph API in C# MVC .NET Core V6

Richard Scannell 341 Reputation points
2023-08-24T15:52:09.7+00:00

I have a .NET MVC core V6 which I am trying to use Azure AD authentication and Graph API with

When I

using MyApp.Models;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using System.Diagnostics;
using Microsoft.Identity.Client;
using Microsoft.Graph;
using Microsoft.AspNetCore.Http;
using Azure.Identity;
using Microsoft.Extensions.Azure;

namespace MyApp.Controllers
{
    public class HomeController : Controller
    {
        private readonly ILogger<HomeController> _logger;

        public HomeController(ILogger<HomeController> logger)
        {
            _logger = logger;
        }
       [Authorize]
        public async Task<IActionResult> Index()
        {
          var userID = User.Identity.Name;
        }

Even though at this point I have not explicitly added Microsoft.Graph via NUGET at this point, online authentication happens, userID gets set , and there is no objection to the presence of the Using Microsoft.Graph; statement at the top.

The fun happens when I try to add syntax from https://developer.microsoft.com/en-us/graph/graph-explorer to get the groups I belong to

The command https://graph.microsoft.com/v1.0/me/transitiveMemberOf/microsoft.graph.group?$count=true suggests the following code

var graphClient = new GraphServiceClient(requestAdapter);

var result = await graphClient.Me.TransitiveMemberOf.GraphGroup.GetAsync((requestConfiguration) =>
{
	requestConfiguration.QueryParameters.Count = true;
	requestConfiguration.Headers.Add("ConsistencyLevel", "eventual");
});

This causes 2 problems :

requestAdapter is not defined and

TransitiveMemberOf does not contain a definition for GraphGroup.

So far I have tried a number of fixes.

  1. changing the graphClient definition to
     var options = new DeviceCodeCredentialOptions
            {
                AuthorityHost = AzureAuthorityHosts.AzurePublicCloud,
                ClientId = clientId,
                TenantId = tenantId,
                 DeviceCodeCallback = (code, cancellation) =>
                {
                    return Task.FromResult(0);
                },
            };
            var deviceCodeCredential = new DeviceCodeCredential(options);
            var graphClient = new GraphServiceClient(deviceCodeCredential, scopes);

fixes the graphClient issue;

  1. Adding Graph.Client explicitly via Nuget gets rid of the error , but causes program.cs to crash in
app.MapControllerRoute(
    name: "default",
    pattern: "{controller=Home}/{action=Index}/{id?}");

with the error 'Unable to load one or more of the requested types.

Could not load type 'Microsoft.Graph.IBaseRequest' from assembly 'Microsoft.Graph.Core, Version=3.0.10.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'.

  1. Removing the graph API via nuget and setting the query syntax to

var result = await graphClient.Me.TransitiveMemberOf.Request().GetAsync();

lets the app build, but it seems to run into a loop , or just really slowly

Any advice on getting this working will be gratefully received. Thanks in advance

Microsoft Graph
Microsoft Graph
A Microsoft programmability model that exposes REST APIs and client libraries to access data on Microsoft 365 services.
12,273 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
11,010 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. TH-4749-MSFT 3,305 Reputation points
    2023-08-24T19:42:21.2133333+00:00

    Hello Richard Scannell,

    Thanks for reaching out. For any Graph SDK related queries please post them on the respective Graph Feedback forum

    Thanks.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.