Share via


教學課程:使用 Service 連線 or 連線 Web 應用程式 Azure 應用程式組態

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

  • 系統指派的受控識別 (SMI)
  • 使用者指派的受控識別 (UMI)
  • 服務主體
  • Connection string

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

  • 設定 Azure 資源
  • 建立 Web 應用程式與 應用程式組態 之間的連線
  • 建置並部署您的應用程式以 Azure App 服務

必要條件

登入 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
    
    參數 描述 範例
    Location 選擇與您接近的位置。 用來 az account list-locations --output table 列出位置。 eastus
    資源群組名稱 您將使用此資源群組來組織完成本教學課程所需的所有 Azure 資源。 service-connector-tutorial-rg
    App Service 名稱 應用程式服務名稱會作為 Azure 中的資源名稱,並以伺服器端點 https://<app-service-name>.azurewebsites.com的形式形成應用程式的完整功能變數名稱。 此名稱在所有 Azure 中都必須是唯一的,且唯一允許的字元為 A-Z、-09 和 。- webapp-appconfig-smi
  3. 建立 Azure 應用程式組態 存放區

    APP_CONFIG_NAME='my-app-config'
    
    az appconfig create -g $RESOURCE_GROUP_NAME -n $APP_CONFIG_NAME --sku Free -l eastus
    
  4. 將測試組態檔匯入至 Azure 應用程式組態。

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

    1. Cd 到資料夾 ServiceConnectorSample

    2. ./sampleconfigs.json 測試組態檔匯入 應用程式組態 存放區。 如果您使用 Cloud Shell,請先上傳 sampleconfigs.json ,再執行 命令。

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

連線 Web 應用程式以 應用程式組態

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

使用系統指派的受控識別驗證,在 Web 應用程式與 應用程式組態 存放區之間建立連線。 此連線是透過 Service 連線 or 來完成。

az webapp connection create appconfig -g $RESOURCE_GROUP_NAME -n $APP_SERVICE_NAME --app-config $APP_CONFIG_NAME --tg $RESOURCE_GROUP_NAME --connection "app_config_smi" --system-identity

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

驗證連線

  1. 若要檢查連線是否正常運作,請從瀏覽器瀏覽至您的 Web 應用程式 https://<myWebAppName>.azurewebsites.net/ 。 網站啟動之後,您會看到它顯示 「Hello」。 您的 Azure WebApp 已透過 Service 連線至 應用程式組態 連線 或現在」 。

運作方式

尋找下列服務 連線 或針對每個驗證類型在幕後管理的內容。

服務 連線 或為您管理聯機組態:

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

如需詳細資訊,請移至服務 連線 或內部。

測試 (選擇性)

或者,執行下列測試:

  1. 更新 應用程式組態 Store 中索引鍵SampleApplication:Settings:Messages的值。

    az appconfig kv set -n <myAppConfigStoreName> --key SampleApplication:Settings:Messages --value hello --yes
    
  2. 移至 並重新整理頁面,以流覽至 https://<myWebAppName>.azurewebsites.net/ 您的 Azure Web 應用程式。 您會看到訊息已更新為 「hello」。

清除資源

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

az group delete -n <myResourceGroupName> --yes

下一步

請遵循下列教學課程,深入瞭解 Service 連線 or。