Connect to Microsoft Graph from a .NET Application

In this Microsoft Graph blog post, I will show how to use the Graph Client to connect to the Graph API from a .NET application.

Microsoft Graph is the REST API that powers Microsoft 365, Azure, and other cloud services. Using the API, we can programmatically create, delete, and manage services and resources across Microsoft Cloud services.

In this post, we will use a C# application to connect to and run a simple program listing all Entra ID users.

Create App Registration

The first step to start with Graph is creating an App Registration. Please visit this post to create one.

The application in this post needs the following API permissions.

  • User.Read.All
  • Directory.Read.All

Note: Copy the tenant ID and application ID and create a secret.

Create a C# Console Application

Create a C# Console application using the following code (create a directory first)

dotnet new console 

Note: Make sure you have .NET 8 installed on your machine first.

Create a Graph Client

.NET connects to Microsoft Graph using a Graph Client. A graph client is an instance of the GraphInstanceClient class.

In your project, create a file called ConnectClient.cs and paste the following code.

using Azure.Identity;
using Microsoft.Graph;
using Microsoft.Extensions.Configuration;

class GraphService
{
  static GraphServiceClient? _client;

  public static GraphServiceClient Client
  {
    get
    {
      if (_client is null)
      {
        var builder = new ConfigurationBuilder().   AddUserSecrets<GraphService>();
        var config = builder.Build();

        var clientId = config["EntraId:ClientId"];
        var clientSecret = config["EntraId:ClientSecret"];
        var tenantId = config["EntraId:TenantId"];

        var credential = new ClientSecretCredential(tenantId, clientId,    clientSecret);
        _client = new GraphServiceClient(credential);
      }

      return _client;
    }
  }
} 

Install Dependencies

Copy the following packages to your .csproj file.

 <ItemGroup>
    <PackageReference Include="Azure.Identity" Version="1.10.4" />
    <PackageReference Include="Microsoft.Extensions.Configuration.UserSecrets" Version="8.0.0" />
    <PackageReference Include="Microsoft.Graph" Version="5.40.0" />
    <PackageReference Include="System.CommandLine" Version="2.0.0-beta4.22272.1" />
  </ItemGroup>

Note: You can also use the dotnet add package command to add them.

Protect Secrets and App Details

Before we can connect and run our application, we need to protect the details the Graph Client needs to connect to Microsoft Graph using Secert Manager.

The details we need to protect are:

  • ClientID
  • Tenant ID
  • Client Secret

Please visit this post to secure the details and come back to the next step (simple steps).

List Entra ID Users

The last step is to copy the following code to the Program.cs file.

// List all Entra ID users

var result = await GraphService.Client.Users.GetAsync();

    foreach(var item in result.Value)
    {
    Console.WriteLine(item.DisplayName);

    }

Save the file and run the application using. dotnet run

Your directory structure should look like this:


Posted

in

, , ,

by

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.