Integrate Microsoft Graph With .NET CORE Web APIs

Introduction

Integrating Microsoft Graph with a .NET Core Web API allows you to leverage Microsoft Graph’s powerful capabilities to access and manipulate data from various Microsoft services, such as Azure Active Directory, Microsoft 365, and more. Here’s a step-by-step guide to integrating Microsoft Graph with a .NET Core Web API:

1. Register Your Application:

Before you can use Microsoft Graph, you need to register your application in the Azure portal:

  • Go to the Azure portal (https://portal.azure.com/).
  • Create a new App Registration.
  • Note down the Application (Client) ID and generate a Client Secret for authentication.

2. Install Required NuGet Packages:

In your .NET Core Web API project, install the necessary NuGet packages:

   dotnet add package Microsoft.Identity.Web
   dotnet add package Microsoft.Graph

3. Configure Authentication:

Configure authentication and authorization by adding the following code to your Startup.cs:

   public void ConfigureServices(IServiceCollection services)
   {
       // ...

       services.AddMicrosoftIdentityWebApiAuthentication(Configuration);

       // ...
   }

In your appsettings.json, add the necessary Azure AD configuration:

   "AzureAd": {
     "Instance": "https://login.microsoftonline.com/",
     "Domain": "yourtenant.onmicrosoft.com",
     "ClientId": "your-client-id",
     "TenantId": "your-tenant-id"
   }
  1. Use Microsoft Graph SDK: In your controller or service, you can use the Microsoft Graph SDK to access data from Microsoft Graph:
   using Microsoft.Graph;
   using Microsoft.Identity.Web;

   // ...

   [Authorize]
   [ApiController]
   [Route("api/[controller]")]
   public class GraphController : ControllerBase
   {
       private readonly GraphServiceClient _graphServiceClient;

       public GraphController(GraphServiceClient graphServiceClient)
       {
           _graphServiceClient = graphServiceClient;
       }

       [HttpGet("me")]
       public async Task<IActionResult> GetMe()
       {
           try
           {
               var me = await _graphServiceClient.Me.Request().GetAsync();
               return Ok(me);
           }
           catch (ServiceException ex)
           {
               return BadRequest(ex.Message);
           }
       }
   }

In this example, the GraphServiceClient is injected into the controller using dependency injection. The GetMe action retrieves the user’s profile information using Microsoft Graph.

  1. Secure Your API: Ensure your Web API is secured and accessible only to authorized users by applying [Authorize] attributes to your controllers or actions.
  2. Testing the API: Run your .NET Core Web API project and use tools like Postman to test the API endpoints. Make sure to obtain an access token (JWT) by authenticating your client application.

Remember to handle errors, implement proper exception handling, and consider using Dependency Injection to manage the GraphServiceClient instance throughout your application.

This guide provides a basic overview of integrating Microsoft Graph with a .NET Core Web API. Depending on your application’s requirements, you can explore more features and endpoints provided by Microsoft Graph to interact with various Microsoft services.

Leave a Reply

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