共用方式為


教學課程:使用服務連接器將 Web 應用程式連線至 Azure 應用程式組態

了解如何使用下列其中一種方法,將 Azure App Service 上執行的 ASP.NET Core 應用程式連線至 Azure 應用程式組態:

  • 系統指派的受控識別 (SMI)
  • 使用者指派的受控識別 (UMI)
  • 服務主體
  • 連線字串

在此教學課程中,您會使用 Azure CLI 來完成下列工作:

  • 設定 Azure 資源
  • 建立 Web 應用程式與應用程式組態之間的連線
  • 建置您的應用程式並將其部署至 Azure App Service

Prerequisites

登入 Azure

在 Azure CLI 中執行 az login 以登入 Azure。

設定 Azure 資源

從建立 Azure 資源開始。

  1. 複製下列範例存放庫:

    git clone https://github.com/Azure-Samples/serviceconnector-webapp-appconfig-dotnet.git
    
  2. 將 Web 應用程式部署至 Azure。

    請遵循下列步驟來建立 App Service 並部署範例應用程式。 確保你有訂閱的 貢獻者擁有者 角色。

    建立應用程式服務,並部署使用系統指派的受控識別來與應用程式組態互動的範例應用程式。

    # Change directory to the SMI sample
    cd serviceconnector-webapp-appconfig-dotnet\system-managed-identity
    
    # Create a web app
    
    LOCATION='eastus'
    RESOURCE_GROUP_NAME='service-connector-tutorial-rg'
    APP_SERVICE_NAME='webapp-appconfig-smi'
    
    az webapp up --location $LOCATION --resource-group $RESOURCE_GROUP_NAME --name $APP_SERVICE_NAME
    
    Parameter Description Example
    Location 選擇與您接近的位置。 使用 az account list-locations --output table 來列出位置。 eastus
    資源群組名稱 使用此資源群組來組織完成本教學課程所需的所有 Azure 資源。 service-connector-tutorial-rg
    App Service 名稱 App Service 名稱會用作 Azure 中的資源名稱,並用以服务器端點形式形成應用程式的完整網域名稱。 有效字元:A-Z0-9-。 Azure 會附加隨機字串,讓伺服器端點在所有 Azure 中是唯一的。 webapp-appconfig-smi
  3. 建立 Azure 應用程式組態存放區。

    APP_CONFIG_NAME='my-app-config'
    
    az appconfig create --resource-group $RESOURCE_GROUP_NAME --name $APP_CONFIG_NAME --sku Free --location eastus
    
  4. 將測試組態檔匯入 Azure 應用程式組態。

    使用系統指派的受控識別,將測試組態檔匯入 Azure 應用程式組態。

    1. 將目錄變更為資料夾 ServiceConnectorSample

    2. ./sampleconfigs.json 測試設定檔匯入 App Configuration Store。 如果你使用 Cloud Shell,執行指令前先上傳 sampleconfigs.json。

      az appconfig kv import --name $APP_CONFIG_NAME --source file --format json --path ./sampleconfigs.json --separator : --yes
      

將 Web 應用程式連線至應用程式組態

建立 Web 應用程式與應用程式組態存放區之間的連線。

使用系統指派的受控識別驗證,在 Web 應用程式和應用程式組態存放區之間建立連線。 此連線會透過服務連接器來完成。

az webapp connection create appconfig --resource-group $RESOURCE_GROUP_NAME --name $APP_SERVICE_NAME --app-config $APP_CONFIG_NAME --tg $RESOURCE_GROUP_NAME --connection "app_config_smi" --system-identity

system-identity 是指系統指派的受控識別 (SMI) 驗證類型。 服務連接器也支援下列驗證:使用者指派的受控識別 (UMI)、連接字串(秘密)和服務主體。

驗證連線

若要檢查連線是否正常運作,請流覽至您的 Web 應用程式。 若要進入 Web 應用程式,最簡單的方式是在 Azure 入口網站中開啟它。 在 概覽 頁面,選擇 預設網域。 網站啟動之後,您會看到它顯示: Hello。您的 Azure WebApp 現在已連線到 ServiceConnector 的應用程式設定。

運作方式

瞭解每個驗證類型的服務連接器在幕後管理的內容。

服務連接器會為您管理連線組態:

  • 設定 Web 應用程式的 AZURE_APPCONFIGURATION_ENDPOINT,讓應用程式存取它並取得應用程式組態端點。 取得 範例程式碼
  • 啟用 Web 應用程式的系統指派的受控驗證,並將授與應用程式組態資料讀取者角色,讓應用程式使用 Azure.Identity 的 DefaultAzureCredential 向應用程式組態進行驗證。 取得 範例程式碼

如需詳細資訊,請參閱 Service Connector 內部結構

測試(可選的)

或者,執行下列測試:

  1. 更新應用程式組態存放區中金鑰 SampleApplication:Settings:Messages 的值。

    az appconfig kv set --name <myAppConfigStoreName> --key SampleApplication:Settings:Messages --value hello --yes
    
  2. 流覽至您的 Azure Web 應用程式,如先前所述,然後重新整理頁面。 你看到訊息更新為: 你好

清除資源

完成之後,如果您不會再使用這些 Azure 資源,請執行 az group delete 命令來刪除。 此命令會刪除您的資源群組及其中的所有資源。

az group delete --name <myResourceGroupName> --yes

後續步驟