Listing all the users and writing them to a file in Azure AD B2C

Image showing users

I had an issue recently where I got an error linking users because that identity was already linked to someone else.

But who was it linked to?

So I looked around for a way to dump all the users and came across this code sample mentioned here to display the attributes of B2C users.

“This .NET Core console application demonstrates the use of the Microsoft Graph API to perform user account management operations (create, read, update, delete) within an Azure AD B2C directory. Also shown is a technique for the bulk import of users from a JSON file. Bulk import is useful in migration scenarios like moving your users from a legacy identity provider to Azure AD B2C”.

The commands are:

Command  Description
[1] Get all users
[2] Get user by object ID
[3] Get user by sign-in name
[4] Delete user by object ID
[5] Update user password
[6] Create users (bulk import)
[7] Create user with custom attributes and show result
[8] Get all users (one page) with custom attributes
[9] Get the number of users in the directory
[help] Show available commands
[exit] Exit the program
Enter command, then press ENTER:

Command “1” to list all the users only displays a few attributes so I wanted to extend it to display more. I also wanted to dump the output to a file so I could search through the results and to format the JSON to make it far more readable.

The result is in this fork.

(Aside: Be aware of this weird error).

Part of the output then looks like:

"identities": [
"issuer": "",
"issuerAssignedId": "123456",
"signInType": "userName"
"issuer": "",
"issuerAssignedId": "",
"signInType": "userPrincipalName"

And it displays this for all the users (and there are a lot!).

It displays the results in a file:

ostrm = new FileStream("./AllUsers.json", FileMode.OpenOrCreate, FileAccess.Write);

which is located here:


You can change the name and location of the file if you want.

Remember to update your settings in appsettings.json.

I was then able to use this utility to dump all the users to a file and then to search for the “issuerAssignedId” in question.

All good!



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Rory Braybrook

Rory Braybrook


NZ Microsoft Identity dude and MVP. Azure AD/B2C/ADFS/Auth0/identityserver. StackOverflow: Presentations: