How to Instantiate GraphServiceClient inside an azure function?

Fareeha Sattar Shaikh 65 Reputation points
2023-03-15T15:34:59.2833333+00:00

Hello,

I am trying to create an azure function which uses graph api to get the authenticated user's profile from azure ad.

I was able to add authentication of microsoft in the azure function app, but now when I try to create GraphServiceClient, all the resources on the internet, are not working in my case. Maybe it is due to the recent update of Microsoft Graph.

I have tried following ways,

//Method 1
var confidentialClientApplication = ConfidentialClientApplicationBuilder
                                                        .Create(Constants.AppId)
                                                        .WithTenantId(Constants.TenantId)
                                                        .WithClientSecret(Constants.ClientSecret)
                                                        .Build();


            ClientCredentialProvider clientCredentialProvider = new ClientCredentialProvider(confidentialClientApplication);

            GraphServiceClient _client = new GraphServiceClient(clientCredentialProvider);

//Method2


 ClientSecretCredential clientSecretCredential = new ClientSecretCredential(Constants.TenantId, Constants.AppId, Constants.ClientSecret);
            GraphServiceClient _client = new GraphServiceClient(clientSecretCredential, Constants.scope); ;



Even the microsoft's documentation suggests the second method, which is not working. By not working I mean, it is not even compiling, there is no such constructor in GraphServiceClient.

Kindly, suggest what should I do?

Azure Functions
Azure Functions
An Azure service that provides an event-driven serverless compute platform.
5,911 questions
Microsoft Security Microsoft Entra Microsoft Entra ID
Microsoft Security Microsoft Graph
0 comments No comments
{count} votes

Accepted answer
  1. CarlZhao-MSFT 46,371 Reputation points
    2023-03-16T06:41:53.16+00:00

    Hi @Fareeha Sattar Shaikh

    Refer to my c# code snippet, it uses client credential provider for authentication, you just need to have Azure.Identity and Microsoft.Graph dependency packages. I've tested it locally and it compiles and runs perfectly.

    using Azure.Identity;  
    using Microsoft.Graph;  
              
    var scopes = new[] { "https://graph.microsoft.com/.default" };  
                
    var tenantId = "tenant id";  
          
    // Values from app registration  
    var clientId = "client id";  
    var clientSecret = "client secret";  
          
    // using Azure.Identity;  
    var options = new TokenCredentialOptions  
    {  
    AuthorityHost = AzureAuthorityHosts.AzurePublicCloud  
    };  
          
    // https://learn.microsoft.com/dotnet/api/azure.identity.clientsecretcredential  
    var clientSecretCredential = new ClientSecretCredential(  
        tenantId, clientId, clientSecret, options);  
    
    // get accessToken          
    var accessToken = await clientSecretCredential.GetTokenAsync(new Azure.Core.TokenRequestContext(scopes) { });  
          
    Console.WriteLine(accessToken.Token);  
       
    var graphClient = new GraphServiceClient(clientSecretCredential, scopes);
    

    Hope this helps.

    If the reply is helpful, please click Accept Answer and kindly upvote it. If you have additional questions about this answer, please click Comment.


0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.