Looking for a .Net Web API sample where I can get all the users and group data from azure ad without microsoft login page for authentication

Shivangi 21 Reputation points
2022-01-19T16:25:49.707+00:00

I am looking for a sample .Net Web API in which I can get the users and groups data from azure ad portal registered under My directory and app registration using microsoft graph without redirecting to microsoft login page for authentication

Microsoft Graph
Microsoft Graph
A Microsoft programmability model that exposes REST APIs and client libraries to access data on Microsoft 365 services.
10,572 questions
Microsoft Entra ID
Microsoft Entra ID
A Microsoft Entra identity service that provides identity management and access control capabilities. Replaces Azure Active Directory.
19,455 questions
{count} votes

Accepted answer
  1. CarlZhao-MSFT 36,891 Reputation points
    2022-01-20T02:58:31.17+00:00

    Hi @Shivangi

    First, grant your application the User.Read.All and Group.Read.All application permissions and grant admin consent. Then you need to use the daemon-based client credential flow to obtain the token, which supports obtaining Azure AD user and group information in an unattended scenario. Here is a sample I write for your reference:

    using Microsoft.Graph;  
    using Microsoft.Graph.Auth;  
    using Microsoft.Identity.Client;  
    using Newtonsoft.Json;  
    using System;  
    using System.Collections.Generic;  
    using System.Net.Http;  
      
    namespace test2  
      
    {  
        class Program  
        {  
            static async System.Threading.Tasks.Task Main(string[] args)  
            {  
                  
                IConfidentialClientApplication app;  
                app = ConfidentialClientApplicationBuilder.Create("{client id}")  
                        .WithClientSecret("{client secret}")  
                        //.WithRedirectUri("https://jwt.ms")  
                        .WithAuthority(new Uri("https://login.microsoftonline.com/{tenant id}"))  
                        .Build();  
      
                AuthenticationResult result;  
      
                string[] scopes = new string[] { "https://graph.microsoft.com/.default" };  
      
                result = await app.AcquireTokenForClient(scopes).ExecuteAsync();  
      
                /*string accesstoken = result.AccessToken;  
          
                Console.WriteLine(accesstoken);*/  
      
                ClientCredentialProvider authProvider = new ClientCredentialProvider(app);  
      
      
                GraphServiceClient graphClient = new GraphServiceClient(authProvider);  
      
                var users = await graphClient.Users  
                    .Request()  
                    .GetAsync();  
      
                Console.WriteLine("users:" + JsonConvert.SerializeObject(users));  
      
            }  
        }  
    }  
    

    By the way, it is not possible to get all user and group information in a single request. I only offer to get the user's information, to get the group's information, you just need to change the code to:

    var groups = await graphClient.Groups  
          .Request()  
          .GetAsync();  
      
    Console.WriteLine("groups:" + JsonConvert.SerializeObject(groups));  
    

    If the answer is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


0 additional answers

Sort by: Most helpful