0

I'm using the MS Graph API to register user on AD B2C, and it works when I tried without adding additional user attributes, but it fails with an error

properties are not available

when I tried adding those user custom attributes.

I did followed other solution thread and added clientid without '-' while setting attribute name in dictionary but doesn't work.

Here is my code snippet:

public class UserService : IUserService
{
    private readonly ILogger<UserService> logger;
    private readonly GraphServiceClient graphServiceClient;
    private readonly string[] supportedRegionCodes;
    private readonly string  _clientId;

    public UserService(ILogger<UserService> logger, IOptions<B2CConfiguration> options)
    {
        this.logger = logger;

        ClientSecretCredentialOptions credentialOptions = new()
        {
            AuthorityHost = AzureAuthorityHosts.AzurePublicCloud,
        };

        ClientSecretCredential clientSecretCredential = new(
            options.Value.TenantId,
            options.Value.ClientId,
            options.Value.ClientSecret,
            credentialOptions);

        _clientId = options.Value.ClientId;

        this.graphServiceClient = new GraphServiceClient(clientSecretCredential, options.Value.Scopes);
        this.supportedRegionCodes = options.Value.SupportedRegionCodes;
    }

    public async Task<string?> RegisterUserToB2C(object userDetails)
    {
        try
        {
            string clientId = _clientId.Replace("-", string.Empty);
            var user = new User
            {
                Identities = new List<ObjectIdentity>
                {
                    new ObjectIdentity
                    {
                        SignInType = "userName",
                        Issuer ="empxxxqa.onmicrosoft.com",
                        IssuerAssignedId = "testuser011"
                    }
                },

                DisplayName = "Test User",
                GivenName = "Test",       
                Surname = "User",
                Mail = "[email protected]",
                OtherMails = new List<string> { "[email protected]" },
                PasswordProfile = new PasswordProfile
                {
                    Password = "@w0rd123!",
                    ForceChangePasswordNextSignIn = true
                },
                UserType = "Member",
                AdditionalData = new Dictionary<string, object>
                    {
                        {$"extension_{clientId}_CardId","TEST"}
                    }
            };

            var created = await graphServiceClient.Users.PostAsync(user);

            return created?.Id;
        }
        catch (Exception ex)
        {
            return ex.Message;
        }
    }
}

This is the error I'm getting:

The following extension properties are not available: extension_3a84xxxxxxxxxxx126f_CardId.

I've also added a screenshot of the property we created in b2c:

enter image description here

I'm relatively new to AD B2C and MS Graph API, do I need to add any thing at the B2C level? As without additional custom user attributes, when I tried to register, it worked, so I believe there should be no permission issues that causing it.

3
  • What is the client ID you are passing in extension_3a84xxxxxxxxxxx126f_CardId? Commented Apr 28 at 11:48
  • 1
    You need to pass b2c-extensions-app client ID as value Commented Apr 28 at 11:56
  • 1
    @Rukmini , i was passing registered app's client id in that, will try with b2c-extension-app client id Commented Apr 28 at 14:33

1 Answer 1

1

To create user using Microsoft Graph API with custom user attributes C#, check the below:

I created a custom user attribute:

enter image description here

Note that: You need to pass Client ID of b2c-extensions-app without hyphen as the Extension ID like below:

enter image description here

extension_<b2cExtensionsAppIDwithouthyphen>_CardId

Now for sample, I used the below code to create user with custom user attribute in Azure AD B2C:

namespace GraphAPIConsoleApp
{
    class Program
    {
        static async Task Main(string[] args)
        {
            // Azure AD B2C credentials
            string tenantId = "TenantID";
            string clientId = "ClientID";
            string clientSecret = "Secret";
            string userSignIn = "testuser011"; // Example user identifier

            // Initialize GraphServiceClient with client credentials
            var clientSecretCredential = new ClientSecretCredential(tenantId, clientId, clientSecret);
            var graphClient = new GraphServiceClient(clientSecretCredential);

            // Create the user with custom extension attribute
            var user = new User
            {
                DisplayName = "Test User",
                GivenName = "Test",
                Surname = "User",
                Mail = "[email protected]",
                OtherMails = new List<string> { "[email protected]" },
                PasswordProfile = new PasswordProfile
                {
                    Password = "@w0rd123!",
                    ForceChangePasswordNextSignIn = true
                },
                UserType = "Member",
                Identities = new List<ObjectIdentity>
                {
                    new ObjectIdentity
                    {
                        SignInType = "userName",
                        Issuer = "infrab2c.onmicrosoft.com", // Change to your B2C domain
                        IssuerAssignedId = "testuser011"
                    }
                },
                // Add custom extension attribute
                AdditionalData = new Dictionary<string, object>
                {
                    { $"extension_<b2cExtensionsAppIDwithouthyphen>_CardId", "123" }
                }
            };

            try
            {
                // Create the user in Azure AD B2C
                var createdUser = await graphClient.Users.PostAsync(user);
                Console.WriteLine($"User created with ID: {createdUser.Id}");
            }
            catch (Exception ex)
            {
                Console.WriteLine($"Error creating user: {ex.Message}");
            }
        }
    }
}

enter image description here

User created successfully:

enter image description here

To verify whether the custom user attribute I ran the user flow and can see the custom user attribute in ID token successfully:

enter image description here

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.