Display User Names From Azure AD using Microsoft Graph API in Windows Forms Application

While working with a client, they recently needed the application to assign permissions to specific folders/subfolders within a document library. To do this, I need to display all user names so the client can select users and grant them permissions to particular folders or subfolders.

In this tutorial, I will show you how to retrieve all user names from Azure Active Directory (Azure AD) using the Microsoft Graph API. This approach allows you to get a list of user names and fetch detailed information about each user stored in Azure AD to the Windows Forms Application.

Get User Name From Azure AD using Microsoft Graph API

To display all user names from Azure Active Directory (Azure AD) in a Windows Forms application, we need to authenticate our app with the Microsoft Graph API using delegated or application permissions.

First, we need to register our application in the Azure Portal to get the Client ID, Tenant ID, and optionally a Client Secret. Then, use the Microsoft Authentication Library (MSAL) to authenticate and acquire a token.

Once authenticated, you can use the Microsoft Graph SDK to call the /users endpoint, which retrieves a list of users from Azure AD.

Register Application in the Azure Portal

I will show you how to get the Client ID, Tenant ID, and Client Secret from the Azure Portal.

  1. Go to the Azure Portal: https://portal.azure.com/ and select the App Registration.
Register an application in Microsoft Entra ID
  1. Select your registered app (or create a new one).
How to do app registration in Azure
  1. Then, the Application client ID and tenant ID will appear on the app overview page.
Display User Names From Azure AD Graph API
  1. To create a Client Secret:
    • Go to Certificates & secrets in the left-hand menu.
    • Click New client secret, add a description and expiry, then click Add.
    • Once created, copy the value immediately; you can’t view it again later.
Registering an Application in Azure Active Directory

In the API permissions section, make sure that the app is granted Microsoft Graph -> User.Read.All application permissions.

Check out How to Use Microsoft Graph API in SPFx Web Parts

Display All User Names from Azure AD in a Windows Forms Application

Now that we have the Client ID, Tenant ID, and secret value, I will show you how to get all user names in Windows from the Application.

  1. Now open the Visual Studio Code comment, create a new project, and select the Windows Forms app (.NET Framework). Then create the App.
Getting User Details from Azure AD in ASP.NET
  1. Then we need to install the required NuGet Packages:
    • Microsoft.Graph
    • Microsoft.Identity.Client
    • Azure.Identity

To install, right-click the application name under the Solution Explorer, then select the Manage NuGet packages. Then go to the Browser tab, search the package name, and install it.

How to display logged user name in Windows Form application using Graph API

Design the Form

Now, we will design the form. I want to display only the name, so I will add a ListBox to the form.

  1. Go to Form Design and, on the left side, under the toolbox, search for and add a ListBox.
How can i get user groups from Azure AD using Windows Forms Application
  1. You can change the list box format using the properties panel, but I only changed the name of the list box to lstUsers here.
Display All User Names from Azure AD in a Windows Forms Application

Add the Code to Get All User Names

Now, double-click the form, and it will open the Form code file where you need to write the below code:

  1. At the top of your Form code file (Form1.cs), add the below Identity SDK.
using Microsoft.Graph;
using Azure.Identity;

These libraries are needed to interact with the Microsoft Graph API (which gives access to Azure AD data) and authenticate your app using Azure credentials.

  1. You can see the using statements below.
using Microsoft.Graph;
using Azure.Identity;
Creating an Azure Active Directory People Picker In Windows Forms Application
  1. Now I will add an asynchronous method that helps me connect to Azure Active Directory and load user information.
private async Task LoadUsersFromAzureAD()
    {
       
}
get user details from azure active directory c#
  1. Inside the method and the below code to Azure AD with our app credentials, and setting up a tool (graphClient) to fetch data like users.
        string tenantId = "YOUR_TENANT_ID";
        string clientId = "YOUR_CLIENT_ID";
        string clientSecret = "YOUR_CLIENT_SECRET";

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

        var graphClient = new GraphServiceClient(credential);
get profile picture from azure active directory c#
  1. Now that the connection to Azure AD is ready using graphClient, we’ll write the code to fetch the users and display their names in the ListBox. Add the following code inside the LoadUsersFromAzureAD() method:
try
{
    var usersPage = await graphClient.Users
        .GetAsync(requestConfig =>
        {
            requestConfig.QueryParameters.Select = new[] { "displayName", "mail" };
            requestConfig.QueryParameters.Top = 100;
        });

    // Loop through paged results
    while (usersPage != null)
    {
        foreach (var user in usersPage.Value)
        {
            lstUsers.Items.Add($"{user.DisplayName} ({user.Mail})");
        }

        if (usersPage.OdataNextLink != null)
        {
            usersPage = await graphClient.Users
                .WithUrl(usersPage.OdataNextLink)
                .GetAsync();
        }
        else
        {
            break;
        }
    }
}
catch (Exception ex)
{
    MessageBox.Show($"Error: {ex.Message}");
}

Here:

  • Sends a request to Microsoft Graph to get a list of users from Azure AD.
  • It only requests the user’s name and email to keep the response fast.
  • If more than 100 users are present, it will automatically continue fetching the next pages using a while loop and OdataNextLink.
  • Each user’s name and email are added to the lstUsers ListBox for easy selection.
  • If there’s any issue, it will show a pop-up error message.
how to get user details from active directory in asp.net core
  1. Then, out of the method, provide the below code to ensure the list loads as soon as the form starts, modify your Form1_Load event like this:
 private void Form1_Load(object sender, EventArgs e)
 {
     LoadUsersFromAzureAD();

 }
Get User Name From Azure AD using Microsoft Graph API
  1. Now, click the Save all button to save the code. To run the application, click Start Without Debugging. You can also click Start to debug the application.
graph api get user by display name
  1. Then, it will open the form, where you can see all the user names and email addresses in the list box.
Display User Name From Azure AD in Windows Forms Application using Microsoft Graph API

In this tutorial, we learned how to connect a Windows Forms application to Azure Active Directory using the Microsoft Graph API. We retrieved all user names and email addresses and displayed them in a ListBox.

You may like the following tutorials:

>

Live Webinar: Quiz Application using SharePoint Framework (SPFx)

Join this free live session and learn how to build a Quiz application using SharePoint Framework (SPFx).

📅 2nd June 2026 – 10:00 AM EST | 7:30 PM IST

Build a High-Performance Project Management Site in SharePoint Online

User registration Power Apps canvas app

DOWNLOAD USER REGISTRATION POWER APPS CANVAS APP

Download a fully functional Power Apps Canvas App (with Power Automate): User Registration App

Power Platform Tutorial FREE PDF Download

FREE Power Platform Tutorial PDF

Download 135 Pages FREE PDF on Microsoft Power Platform Tutorial. Learn Now…