在部署 Azure 受控應用程式時存取 Key Vault 秘密

當您需要在部署期間,傳送安全值 (例如密碼) 做為參數時,您可以從 Azure Key Vault 擷取值。 若要在部署受控應用程式時存取 Key Vault,您必須授與設備資源提供者服務主體的存取權。 受控應用程式服務會使用這個身分識別來執行作業。 若要在部署期間成功地從 Key Vault 擷取值,服務主體必須要能夠存取 Key Vault。

本文說明如何設定與受控應用程式搭配使用的 Key Vault。

啟用範本部署

  1. 登入 Azure 入口網站

  2. 開啟您的金鑰保存庫。 在搜尋方塊中輸入金鑰保存庫,或選取 [金鑰保存庫]

    Screenshot of the Azure home page to open a key vault using search or by selecting key vault.

  3. 選取 [存取設定]

    Screenshot of the key vault setting to select access configuration.

  4. 選取 [用於範本部署的 Azure Resource Manager]。 然後,選取 [套用]

    Screenshot of the key vault's access configuration that enables Azure Resource Manager for template deployment.

將服務新增為參與者

將 [參與者] 角色指派給金鑰保存庫範圍的 [設備資源提供者] 使用者。 參與者角色是角色指派的特殊權限系統管理員角色。 如需詳細步驟,請前往使用 Azure 入口網站指派 Azure 角色

設備資源提供者是 Microsoft Entra 租用戶中的服務主體。 您可以從 Azure 入口網站前往 [Microsoft Entra ID]>[企業應用程式],並將搜尋篩選條件變更為 [Microsoft 應用程式],以驗證其註冊狀態。 搜尋設備資源提供者。 如果找不到,請註冊Microsoft.Solutions 資源提供者。

參考 Key Vault 祕密

若要將 Key Vault 中的祕密傳至受控應用程式中的範本,您必須使用連結或巢狀範本,並參考連結或巢狀範本參數中的 Key Vault。 請提供 Key Vault 的資源識別碼和秘密的名稱。

{
  "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "parameters": {
    "location": {
      "type": "string",
      "defaultValue": "[resourceGroup().location]",
      "metadata": {
        "description": "The location where the resources will be deployed."
      }
    },
    "vaultName": {
      "type": "string",
      "metadata": {
        "description": "The name of the key vault that contains the secret."
      }
    },
    "secretName": {
      "type": "string",
      "metadata": {
        "description": "The name of the secret."
      }
    },
    "vaultResourceGroupName": {
      "type": "string",
      "metadata": {
        "description": "The name of the resource group that contains the key vault."
      }
    },
    "vaultSubscription": {
      "type": "string",
      "defaultValue": "[subscription().subscriptionId]",
      "metadata": {
        "description": "The name of the subscription that contains the key vault."
      }
    }
  },
  "resources": [
    {
      "type": "Microsoft.Resources/deployments",
      "apiVersion": "2022-09-01",
      "name": "dynamicSecret",
      "properties": {
        "mode": "Incremental",
        "expressionEvaluationOptions": {
          "scope": "inner"
        },
        "template": {
          "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
          "contentVersion": "1.0.0.0",
          "parameters": {
            "adminLogin": {
              "type": "string"
            },
            "adminPassword": {
              "type": "securestring"
            },
            "location": {
              "type": "string"
            }
          },
          "variables": {
            "sqlServerName": "[concat('sql-', uniqueString(resourceGroup().id, 'sql'))]"
          },
          "resources": [
            {
              "type": "Microsoft.Sql/servers",
              "apiVersion": "2022-05-01-preview",
              "name": "[variables('sqlServerName')]",
              "location": "[parameters('location')]",
              "properties": {
                "administratorLogin": "[parameters('adminLogin')]",
                "administratorLoginPassword": "[parameters('adminPassword')]"
              }
            }
          ],
          "outputs": {
            "sqlFQDN": {
              "type": "string",
              "value": "[reference(variables('sqlServerName')).fullyQualifiedDomainName]"
            }
          }
        },
        "parameters": {
          "location": {
            "value": "[parameters('location')]"
          },
          "adminLogin": {
            "value": "ghuser"
          },
          "adminPassword": {
            "reference": {
              "keyVault": {
                "id": "[resourceId(parameters('vaultSubscription'), parameters('vaultResourceGroupName'), 'Microsoft.KeyVault/vaults', parameters('vaultName'))]"
              },
              "secretName": "[parameters('secretName')]"
            }
          }
        }
      }
    }
  ],
  "outputs": {
  }
}

下一步

您已將 Key Vault 設定為在受控應用程式部署期間可供存取。