azureazure-active-directoryazure-ad-b2c-custom-policyidentity-experience-framework

b2c tenant - automate custom policy under identity experience framework


I am trying to automate b2c tenant custom policy creation under identity experience framework. Go to Azure Portal -> Azure AD B2C -> Policies -> Identity Experience Framework

But mg.graph module is giving pain while importing.

error: Import-Module -Name Microsoft.Graph -Force Import-Module : Function Find-MgTenantRelationshipTenantInformationByDomainName cannot be created because function capacity 4096 has been exceeded for this scope.

Note: I tried this in multiple PCs and ended up with same error.

# Authenticate to Microsoft Graph using client credentials
try {
    $secureClientSecret = ConvertTo-SecureString -String $clientSecret -AsPlainText -Force
    $credential = New-Object -TypeName Microsoft.Graph.PowerShell.Authentication.Credential
    $credential.ClientId = $clientId
    $credential.ClientSecret = $secureClientSecret
    $credential.TenantId = $tenantName

    Connect-MgGraph -Credential $credential -Scopes "Policy.ReadWrite.TrustFramework"
    Write-Host "Successfully authenticated to Microsoft Graph."
} catch {
    Write-Output "Failed to authenticate to Microsoft Graph, error: $($_.Exception.Message)"
    throw
}

# Upload the policy file
try {
    $policyName = "SignUpOrSignin-ODP-D2.xml"
    $policyContent = Get-Content -Path $outputFilePath -Raw

    Write-Host "Uploading policy: $policyName..."

    $response = Invoke-MgGraphRequest -Uri "https://graph.microsoft.com/v1.0/trustFramework/policies/$policyName" `
                                      -Method PUT `
                                      -Body $policyContent `
                                      -Headers @{ "Content-Type" = "application/xml" }

    if ($response) {
        Write-Host "Successfully uploaded policy: $policyName"
    } else {
        Write-Host "Failed to upload policy: $policyName"
    }
} catch {
    Write-Output "Failed to upload the policy file, error: $($_.Exception.Message)"
    throw
}

I am trying to automate it to use powershell through azuredevops with a single without user interaction.

Thanks Krupa G


Solution

  • Trying to automate b2c tenant custom policy creation under identity experience framework.

    The error message you get might because of too many modules are installed in your system or the same module might be called multiple times.

    Ensure that your SignUpOrSignIn.xml has PolicyId:

    enter image description here

    For adding SignUpOrSignIn.xml to Identity Experience Framework.

    Use below Modified Powershell Script

    # Connect to Microsoft Graph with required permissions
    
    #Import-Module Microsoft.Graph -Force
    
    Connect-MgGraph -Scopes "Policy.ReadWrite.TrustFramework"
    
    # Define policy details
    $policyId = "B2C_1A_signup_signin-Demo"
    $policyFile = "<YOUR_FILE_PATH\SignUpOrSignin.xml"
    $outputFilePath = "<RESPONSE_OUTPUT>\policy-upload-response.xml"
    
    # Verify if the file exists
    if (-Not (Test-Path $policyFile)) {
        Write-Host "Error: Policy file not found at $policyFile" -ForegroundColor Red
        exit
    }
    
    # Read policy content
    $policyContent = Get-Content -Path $policyFile -Raw
    
    Write-Host "Uploading policy: $policyId.xml"
    
    # Upload the policy to Microsoft Graph
    try {
        Invoke-MgGraphRequest -Uri "https://graph.microsoft.com/beta/trustFramework/policies/$policyId/`$value" `
                              -Method PUT `
                              -Body $policyContent `
                              -Headers @{ "Content-Type" = "application/xml" } `
                              -OutputFilePath $outputFilePath  # This saves the response
    
        Write-Host "Successfully uploaded policy: $policyId.xml" -ForegroundColor Green
        Write-Host "Response saved to: $outputFilePath"
    } catch {
        Write-Host "Failed to upload policy: $policyId.xml. Error: $($_.Exception.Message)" -ForegroundColor Red
    }
    
    Write-Host "Policy upload process completed!"
    
    

    Response:

    enter image description here

    Also, I've verified from the portal by Navigating to Azure Portal -> Azure AD B2C -> Policies -> Identity Experience Framework

    enter image description here

    If still issue persists update the graph modules to latest version using Update-Module Microsoft.Graph and try the same.

    Reference:

    Azure AD B2C