Search
Close this search box.

This platform stands as a cornerstone for developers, providing a streamlined gateway to an extensive range of data and functionalities. It elevates collaboration and productivity to new heights. By bridging users, devices, and data within the Microsoft landscape, Microsoft Graph has established itself as a critical tool for today’s businesses and individuals. 

Create user account in Azure AD B2C

To make a new user account, the PostAsync function of the Microsoft Graph client will be employed.

				
					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 = "rahat036@gmail.com"
                }
            },
            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();
    }
}
				
			

To make a new user, give the user object to the PostAsync method

User features

  • GivenName – This is the firstname
  • Surname – Lastname
  • Identities – This is a list of identity objects, where SignInType is an email address, Issue is a tenantId, and IssuesAssignedId is an email address
  • PasswordProfile – PasswordProfile object, set a temporary password for the user.

Run the program. It will successfully add a new user to Azure AD B2C directory. 

About Author

Rezwanur Rahman

Rezwanur Rahman is the Microsoft Graph MVP, located in Innsbruck, Austria. He is the ex-Microsoft employee at Microsoft Bangladesh, and Microsoft Technical Support Lead for Microsoft 365 Global Support. He is a software engineer graduate and currently contributing technical knowledge on Microsoft Copilot and ChatGPT.