Graph API in C# MVC .NET Core V6

Richard Scannell 321 Reputation points

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;
        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 to get the groups I belong to

The command$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
    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=, 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.
10,533 questions
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.
10,209 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. TH-4749-MSFT 3,290 Reputation points

    Hello Richard Scannell,

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


    0 comments No comments