Creating users from another Tenant in B2C.

Kevin Dule 65 Reputation points
2024-08-26T11:18:17.8633333+00:00

Hello @ everyone

I am writing you about a problem that we are facing in our B2C sign in policy. So we have define two methods of login, one of them is using an Azure Entra ID. Also we have to create users that have to access this app in B2C Tenant. I have prepared a PSH script using MicrosoftGraph for creating in B2C Tenant users from Azure Entra ID.


# Connect to Microsoft Graph
Connect-MgGraph -Scopes "User.ReadWrite.All"

# Path to your CSV file
$csvPath = "C:\PathToCSVEntraIDUsersToB2C"

# Read users from CSV file
$userList = Import-Csv -Path $csvPath

# List to store created users and their passwords
$createdUsers = @()

# Function to generate a random 15-character password
function Generate-RandomPassword {
    $length = 15
    $chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789!@#$%^&*()"
    $password = -join ((1..$length) | ForEach-Object { $chars[(Get-Random -Maximum $chars.Length)] })
    return $password
}

# Loop through each user in the CSV file
foreach ($user in $userList) {
    $email = $user.Email
    
    # Generate a unique ObjectId for the user
    $guid = [guid]::NewGuid().ToString()

    # Generate a random 15-character password
    $randomPassword = Generate-RandomPassword

    # Define the new identities to be added for each user
    $identities = @(
        @{
            signInType = "federated";
            issuer = "https://login.microsoftonline.com/XXXXX-XXXXX-XXXXXX-XXXXX/v2.0";
            issuerAssignedId = $guid;
        }
    )

    # Define the user creation parameters
    $userParams = @{
        AccountEnabled = $true
        DisplayName = $email
        MailNickname = $email.Split('@')[0]
        UserPrincipalName = "cpim_$guid@YYYYY.onmicrosoft.com" # Set UserPrincipalName with cpim_ prefix
        Identities = $identities
        PasswordProfile = @{
            ForceChangePasswordNextSignIn = $true
            Password = $randomPassword # Use generated password
        }
    }

    try {
        # Create the user
        New-MgUser @userParams
        Write-Host "Successfully created user: $email"
        
        # Store the created user and password
        $createdUsers += [PSCustomObject]@{
            Email = $email
            Password = $randomPassword
        }
    } catch {
        Write-Host "Failed to create user: $email. Error: $_"
    }
}

# Print the list of created users and their passwords
Write-Host "Created Users and Their Passwords:"
$createdUsers | Format-Table -AutoSize

Script is executed succesfully and users are created okay/

But it is same error:

error_description: AADB2C99002: User does not exist. Please sign up before you can sign in.

What do you suggest to me?

Microsoft Graph
Microsoft Graph
A Microsoft programmability model that exposes REST APIs and client libraries to access data on Microsoft 365 services.
12,265 questions
Microsoft Entra ID
Microsoft Entra ID
A Microsoft Entra identity service that provides identity management and access control capabilities. Replaces Azure Active Directory.
22,092 questions
{count} votes

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.