C# How to Call Graph API from Web API using MSAL Authentication


In this article you will see, in C# how to call Graph API from Web API using MSAL Auth using .NET Framework. MSAL auth with Graph API works good on Console app, Azure Func App, etc. But not for Web API, need to handle with Azure Identity. 

To make it work on Web API, instead of using Microsoft.Client.Identity we will need to Azure.Identity. Internally Azure.Identity uses the MSAL authentication. So you can use like below.

var tenantId = "xxxxx-xxxx-4392-xxxx-xxxxxxxxxx";

var clientId = "xxxxx-xxxx-4203-xxxx-xxxxxxxx";

var clientSecret = "xxxxxzzxxzzzxxzzzzzxxxxxxxx";

var clientSecretCredential = new ClientSecretCredential(tenantId, clientId, clientSecret);

GraphServiceClient graphServiceClient = new GraphServiceClient(clientSecretCredential);

var users = graphServiceClient.Users.Request()

                  .Select(x => x.DisplayName).GetAsync().Result;

Output: You will get result like below.


Happy PC (Programming / Configuring)

Comments

Popular posts from this blog

In C# CSOM How to Delete Folders Recursively, Sub-Folders, Files in SharePoint Online Document Library

How Get, Set, Delete Permission on SharePoint Online Site using Graph API

What is SharePoint online default authentication method? And which credential flow it is using to authenticate?