這很重要
自 2025 年 5 月 1 日起,Azure AD B2C 將不再可供新客戶購買。 在我們的常見問題中深入瞭解。
Azure Pipelines 支持持續整合 (CI) 和持續交付 (CD),以持續一致地測試、生成代碼並將其交付到任何目標。 本文介紹如何使用 Azure Pipelines 自動執行 Azure Active Directory B2C (Azure AD B2C) 自定義策略 的部署過程。
這很重要
透過 Azure Pipelines 管理 Azure AD B2C 自訂政策,目前會在 Microsoft Graph API 端點上使用/beta作業。 不支援在生產應用程式中使用這些 API。 如需詳細資訊,請參閱 Microsoft Graph REST API Beta 端點參考。
先決條件
- 完成開始使用 Active Directory B2C 中的自定義原則中的步驟。
- 如果尚未創建 DevOps 組織,請按照 註冊、登錄到 Azure DevOps 中的說明創建一個。
為管理任務註冊應用程式
使用 PowerShell 腳本部署 Azure AD B2C 策略。 在 PowerShell 腳本可以與 Microsoft Graph API 交互之前,請在 Azure AD B2C 租戶中創建應用程式註冊。 如果您尚未這麼做, 請註冊 Microsoft Graph 應用程式。
要使 PowerShell 腳本訪問 MS Graph 中的數據,請向已註冊的應用程式授予相關的 應用程式許可權。 在應用註冊的 API 許可權中授予 >>Policy.ReadWrite.TrustFramework 許可權。
配置 Azure 儲存庫
註冊 Microsoft Graph 應用程式後,您就可以為策略檔配置存儲庫了。
- 登入您的 Azure DevOps 組織。
- 創建新專案,或選擇現有專案。
- 在專案中,導航到 「存儲庫」,然後選擇「 檔案」。
- 選擇現有儲存庫或創建一個儲存庫。
- 在資料庫的根目錄中,建立一個名為
B2CAssets的資料夾。 將 Azure AD B2C 自定義策略檔添加到 B2CAssets 資料夾。 - 在資料庫的根目錄中,建立一個名為
Scripts的資料夾。 創建 PowerShell 文件 DeployToB2C.ps1. 將以下 PowerShell 腳本粘貼到 DeployToB2C.ps1中。 - 提交 並 推送 變更。
以下腳本從 Microsoft Entra ID 獲取訪問令牌。 使用驗證令牌,腳本透過 MS Graph API 上傳位於 B2CAssets 資料夾內的政策。 您還可以在上傳策略之前更改策略的內容。 例如,將 替換為 tenant-name.onmicrosoft.com 您的租戶名稱。
[Cmdletbinding()]
Param(
[Parameter(Mandatory = $true)][string]$ClientID,
[Parameter(Mandatory = $true)][string]$ClientSecret,
[Parameter(Mandatory = $true)][string]$TenantId,
[Parameter(Mandatory = $true)][string]$Folder,
[Parameter(Mandatory = $true)][string]$Files
)
try {
$body = @{grant_type = "client_credentials"; scope = "https://graph.microsoft.com/.default"; client_id = $ClientID; client_secret = $ClientSecret }
$response = Invoke-RestMethod -Uri https://login.microsoftonline.com/$TenantId/oauth2/v2.0/token -Method Post -Body $body
$token = $response.access_token
$headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
$headers.Add("Content-Type", 'application/xml')
$headers.Add("Authorization", 'Bearer ' + $token)
# Get the list of files to upload
$filesArray = $Files.Split(",")
Foreach ($file in $filesArray) {
$filePath = $Folder + $file.Trim()
# Check if file exists
$FileExists = Test-Path -Path $filePath -PathType Leaf
if ($FileExists) {
$policycontent = Get-Content $filePath -Encoding UTF8
# Optional: Change the content of the policy. For example, replace the tenant-name with your tenant name.
# $policycontent = $policycontent.Replace("your-tenant.onmicrosoft.com", "contoso.onmicrosoft.com")
# Get the policy name from the XML document
$match = Select-String -InputObject $policycontent -Pattern '(?<=\bPolicyId=")[^"]*'
If ($match.matches.groups.count -ge 1) {
$PolicyId = $match.matches.groups[0].value
Write-Host "Uploading the" $PolicyId "policy..."
$graphuri = 'https://graph.microsoft.com/beta/trustframework/policies/' + $PolicyId + '/$value'
$content = [System.Text.Encoding]::UTF8.GetBytes($policycontent)
$response = Invoke-RestMethod -Uri $graphuri -Method Put -Body $content -Headers $headers -ContentType "application/xml; charset=utf-8"
Write-Host "Policy" $PolicyId "uploaded successfully."
}
}
else {
$warning = "File " + $filePath + " couldn't be not found."
Write-Warning -Message $warning
}
}
}
catch {
Write-Host "StatusCode:" $_.Exception.Response.StatusCode.value__
$_
$streamReader = [System.IO.StreamReader]::new($_.Exception.Response.GetResponseStream())
$streamReader.BaseStream.Position = 0
$streamReader.DiscardBufferedData()
$errResp = $streamReader.ReadToEnd()
$streamReader.Close()
$ErrResp
exit 1
}
exit 0
配置 Azure Pipelines
初始化儲存庫並填充自定義策略檔后,您就可以設置發佈管道了。 要創建管道,請執行以下步驟:
- 在您的專案中,選擇 Pipelines>Releases>。
- 在 Select a template (選擇範本) 下,選擇 Empty job (空作業),然後選擇 Apply (應用)。
- 輸入 Stage name (階段名稱),例如 DeployCustomPolicies,然後關閉窗格。
- 選擇「添加專案」,然後在「源類型」下選擇「Azure 存儲庫」。。
- 對於專案,選擇您的專案。
- 選擇包含 Scripts 資料夾的 Source (repository) (源(儲存庫))。
- 選擇 Default branch(預設分支),例如 master。
- 保留預設分支中的 Latest (最新) 的 Default version (預設版本) 設定。
- 輸入存儲庫的 Source別名 。 例如, policyRepo。
- 選取 [] 和 新增 []
- 重命名管道以反映其意圖。 例如, 部署自定義策略管道。
- 選擇 Save 以保存管道配置。
配置管道變數
管道變數為您提供了一種將關鍵數據導入管道各個部分的便捷方法。 以下變數提供有關 Azure AD B2C 環境的資訊。
| 名稱 | 價值觀 |
|---|---|
clientId |
您之前註冊的應用程式之客戶端 ID。 |
clientSecret |
您之前建立的 用戶端金鑰 的值。 將變數類型更改為 secret (選擇鎖圖示)。 |
tenantId |
your-b2c-tenant.onmicrosoft.com,其中 your-b2c-tenant 是 Azure AD B2C 租戶的名稱。 |
要添加管道變數,請執行以下步驟:
- 在您的管道中,選擇變數(Variables)標籤。
- 在 Pipeline variables (管道變數) 下,添加上述變數及其值。
- 選擇 Save 以保存變數。
添加管道任務
管道任務是執行某項操作的預先打包腳本。 新增一個執行 DeployToB2C.ps1 PowerShell 腳本的任務。
在您創建的管道中,選擇任務選項卡。
選擇 Agent job (代理作業),然後選擇加號 ()+ 以將任務添加到代理作業。
搜索並選擇 PowerShell。 不要選擇“Azure PowerShell”、“目標計算機上的 PowerShell”或其他 PowerShell 條目。
選擇新添加的 PowerShell 腳本 任務。
為 PowerShell 命令稿任務輸入以下值:
任務版本:2.*
Display name(顯示名稱):此任務應上傳的策略的名稱。 例如, B2C_1A_TrustFrameworkBase。
類型:文件路徑
腳本路徑:選擇省略號 (...),導航到 Scripts 資料夾,然後選擇 DeployToB2C.ps1 檔。
參數:輸入下面的 PowerShell 腳本。
-ClientID $(clientId) -ClientSecret $(clientSecret) -TenantId $(tenantId) -Folder $(System.DefaultWorkingDirectory)/policyRepo/B2CAssets/ -Files "TrustFrameworkBase.xml,TrustFrameworkLocalization.xml,TrustFrameworkExtensions.xml,SignUpOrSignin.xml,ProfileEdit.xml,PasswordReset.xml"該
-Files參數是要部署的策略檔的逗號分隔符清單。 請將您的策略檔案新增至清單以進行更新。這很重要
確保策略按正確的順序上傳。 首先是基本策略、擴展策略,然後是依賴方策略。 例如:
TrustFrameworkBase.xml,TrustFrameworkLocalization.xml,TrustFrameworkExtensions.xml,SignUpOrSignin.xml。
選擇 Save 以保存代理作業。
測試管道
要測試發布流程,請執行以下步驟:
- 選擇 Pipelines ,然後選擇 Releases 。
- 選擇您之前創建的管道,例如 DeployCustomPolicies。
- 選擇 創建發佈,然後選擇 創建 以將發佈排隊。
您應該會看到一個通知橫幅,指出發行版已排隊。 要查看其狀態,請選擇通知橫幅中的連結,或在 Releases 選項卡的清單中選擇它。
後續步驟
深入瞭解: