Getting Microsoft graph client in v5+

-- -- 957 Reputation points
2023-04-29T07:10:48.3066667+00:00

Hi

How can I get Graph client in v5+ using

clientId/applicationid,

tenantId, and

clientSecret

for a without-user access?

Thanks

Regards

Microsoft Security Microsoft Graph
{count} votes

2 answers

Sort by: Most helpful
  1. Antonio 250 Reputation points Microsoft External Staff
    2023-05-04T20:39:09.0433333+00:00

    Thanks for posting in Q&A forum.

    For the statement "graph v5+", this seems to indicate for Microsoft Graph API SDK version 5 and above.

    If so, we have a tutorial written with .NET SDK version 7.0.102. for listing users in a tenant using .NET Secret Manager for the client secret.

    However, please note that you must have Global/Azure Admin role to create the Azure App and client secret.

    --please don't forget to upvote and Accept as answer if the reply is helpful--

    0 comments No comments

  2. CarlZhao-MSFT 46,371 Reputation points
    2023-05-05T05:47:58.2566667+00:00

    Hi @-- --

    Refer to the sample code for getting the graph client using the client credential flow:

    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://docs.microsoft.com/dotnet/api/azure.identity.clientsecretcredential
    var clientSecretCredential = new ClientSecretCredential(
          tenantId, clientId, clientSecret, options);
      
    var graphClient = new GraphServiceClient(clientSecretCredential, scopes);
    

    Dependency package:

    User's image

    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 comments No comments

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.