Get All Azure AD B2C Applications using MS Graph API

Introduction

This powerful platform enables developers to access data and functionalities seamlessly, fostering enhanced collaboration and productivity. With its ability to connect users, devices, and data across the Microsoft ecosystem, Microsoft Graph has become an indispensable tool for modern businesses and individuals.

This continues my previous article on Azure AD B2C with MS Graph. I highly recommend you go through my last article before getting into it.

Get All Azure AD B2C applications

Before getting into a code, ensure you have added the required API scope permission in Azure AD B2C app registration for our client credential flow.

Since we are using a client credential flow, the below permissions are the least privilege to read all application details.

Application.Read.And you also need a Directory.Read.All

Web API

public static async Task ListAllApplication(GraphServiceClient graphClient)
{
    try
    {
        var apps = await graphClient.Applications.GetAsync();
        var pageIterator = PageIterator<Application, ApplicationCollectionResponse>
           .CreatePageIterator(
               graphClient,
               apps,
               (app) =>
               {
                   Console.WriteLine(JsonSerializer.Serialize(apps));
                   return true;
               },
               (req) =>
               {
                   Console.WriteLine($"Reading next page of apps...");
                   return req;
               }
           );

        await pageIterator.IterateAsync();
    }
    catch (ServiceException ex)
    {
        Console.WriteLine($"Error getting applications: {ex.Message}");
    }   
}

The above function will return all Azure AD B2C applications from the directory, you can extend this function and get the application details where the client secrets are going to expire, this is one of the main use cases where you can leverage it.

Summary

We have seen how to get all Azure AD B2C applications using Microsoft Graph API. We will see more about creating an alert when the application client secret expires in my next blog.

Click here to download the source.