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.
Comments
Post a Comment