You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Azure AD B2C批量导入异域名本地账户报错,咨询解决方法

Azure AD B2C批量导入外部域名用户解决方案

问题根源

默认的Azure AD批量导入工具是针对内部AD用户设计的,要求userPrincipalName的域名必须是租户已验证的域名。而手动创建B2C用户时,后台会自动标记该用户为本地账户(LocalAccount),允许使用外部域名(如@gmail.com、@company.com)作为UPN。批量CSV模板没有提供标识B2C用户的字段,因此会触发域名验证报错。

解决方法

方法1:使用Microsoft Graph API批量创建

通过Graph API可以明确指定用户为B2C本地账户,绕过域名验证限制,步骤如下:

  • 构造批量请求,每个用户请求需包含以下关键字段:
    • creationType: 设置为LocalAccount(核心标识,告诉系统这是B2C用户)
    • signInType: 根据B2C配置选择emailAddress或userName
    • userPrincipalName: 直接使用外部域名(如john.doe@gmail.com)
    • passwordProfile: 包含初始密码及强制修改设置(本地账户必填)
  • 批量请求示例JSON:
{
  "requests": [
    {
      "id": "1",
      "method": "POST",
      "url": "/users",
      "body": {
        "accountEnabled": true,
        "displayName": "John Doe",
        "mailNickname": "johndoe",
        "userPrincipalName": "john.doe@gmail.com",
        "signInType": "emailAddress",
        "creationType": "LocalAccount",
        "passwordProfile": {
          "forceChangePasswordNextSignIn": true,
          "password": "TempPass123!"
        }
      },
      "headers": {
        "Content-Type": "application/json"
      }
    },
    {
      "id": "2",
      "method": "POST",
      "url": "/users",
      "body": {
        "accountEnabled": true,
        "displayName": "Jane Smith",
        "mailNickname": "janesmith",
        "userPrincipalName": "jane.smith@company.com",
        "signInType": "emailAddress",
        "creationType": "LocalAccount",
        "passwordProfile": {
          "forceChangePasswordNextSignIn": true,
          "password": "TempPass456!"
        }
      },
      "headers": {
        "Content-Type": "application/json"
      }
    }
  ]
}
  • 针对dev、uat、prd三个租户,分别调用对应租户的Graph端点,确保拥有User.ReadWrite.All权限。

方法2:PowerShell脚本处理CSV批量导入

如果已有CSV用户列表,可通过PowerShell结合Graph模块批量创建,步骤如下:

  1. 安装Microsoft Graph PowerShell模块:
Install-Module Microsoft.Graph -Force
  1. 编写脚本读取CSV并创建用户(示例):
# 连接目标租户(替换为dev/uat/prd的租户ID或域名)
Connect-MgGraph -TenantId "dev.onmicrosoft.com" -Scopes "User.ReadWrite.All"

# 读取CSV文件(确保CSV包含displayName, mailNickname, userPrincipalName, signInType, password列)
$userList = Import-Csv -Path "./b2c-users.csv"

foreach ($user in $userList) {
    $createParams = @{
        AccountEnabled = $true
        DisplayName = $user.displayName
        MailNickname = $user.mailNickname
        UserPrincipalName = $user.userPrincipalName
        SignInType = $user.signInType
        CreationType = "LocalAccount"
        PasswordProfile = @{
            ForceChangePasswordNextSignIn = $true
            Password = $user.password
        }
    }

    # 创建用户
    try {
        New-MgUser @createParams
        Write-Host "Successfully created user: $($user.userPrincipalName)"
    }
    catch {
        Write-Host "Failed to create user $($user.userPrincipalName): $_"
    }
}
  1. 针对三个租户分别修改TenantId参数,运行脚本即可完成批量导入。

内容的提问来源于stack exchange,提问作者Aldo Inácio da Silva

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.26 12:15:12