Create user account in Azure AD B2C with MS Graph

Introduction

This powerful platform enables developers to access a wealth of 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 alike. 

This is a continuation of my previous article on Azure AD B2C with MS Graph. I highly recommend you go through my previous article before getting into it.

Create user account in Azure AD B2C

The PostAsync function from the MS Graph client will be used to create a new user account. 

public static async Task CreateUser(GraphServiceClient graphClient,string tenantId)
{
    try
    {
        // Create user
        var result = await graphClient.Users
        .PostAsync(new User
        {
            GivenName = "Test",
            Surname = "B2C",
            DisplayName = "Test B2C",
            Identities = new List<ObjectIdentity>
            {
                new ObjectIdentity()
                {
                    SignInType = "emailAddress",
                    Issuer = tenantId,
                    IssuerAssignedId = "[email protected]"
                }
            },
            PasswordProfile = new PasswordProfile()
            {
                Password = Helpers.PasswordHelper.GenerateNewPassword(4, 8, 4)
            },
            PasswordPolicies = "DisablePasswordExpiration",
                    
        });

        string userId = result.Id;

        Console.WriteLine($"Created the new user. Now get the created user with object ID '{userId}'...");

        // Get created user by object ID
        result = await graphClient.Users[userId]
            .GetAsync();

        if (result != null)
        {
            Console.ForegroundColor = ConsoleColor.Blue;
            Console.WriteLine($"DisplayName: {result.DisplayName}");
            Console.WriteLine();
            Console.ResetColor();
            Console.WriteLine(JsonSerializer.Serialize(result, new JsonSerializerOptions { WriteIndented = true }));
        }
    }
     catch (Exception ex)
    {
        Console.ForegroundColor = ConsoleColor.Red;
        Console.WriteLine(ex.Message);
        Console.ResetColor();
    }
}

Pass the user object to the PostAsync method to create a user,

User attributes  

  1. GivenName – It’s the firstname 
  2. Surname – Lastname
  3. Identities – It’s a list of object identity, where SignInType is an email address, Issue is a tenantId, and IssuesAssignedId is an email address 
  4. PasswordProfile – PasswordProfile object, assign a temporary password for the user. 

Execute the program. It will successfully create a new user in Azure AD B2C directory. 

Summary

We have seen how to create a user account in Azure AD B2C with Microsoft Graph API, will see more about creating a user with the custom attributes in my next blog. 

Click here to download the source.