了解如何使用下列其中一種方法,將 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 資源開始。
複製下列範例存放庫:
git clone https://github.com/Azure-Samples/serviceconnector-webapp-appconfig-dotnet.git
將 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-Z、0-9 和 -。 Azure 會附加隨機字串,讓伺服器端點在所有 Azure 中是唯一的。 |
webapp-appconfig-smi |
建立應用程式服務,並部署使用使用者指派的受控識別來與應用程式組態互動的範例應用程式。
# Change directory to the UMI sample
cd serviceconnector-webapp-appconfig-dotnet\user-assigned-managed-identity
# Create a web app
LOCATION='eastus'
RESOURCE_GROUP_NAME='service-connector-tutorial-rg'
APP_SERVICE_NAME='webapp-appconfig-umi'
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-Z、0-9 和 -。 Azure 會附加隨機字串,讓伺服器端點在所有 Azure 中是唯一的。 |
webapp-appconfig-umi |
建立使用者指派的受控識別。 將輸出儲存到臨時記事本。
az identity create --resource-group $RESOURCE_GROUP_NAME --name "myIdentity"
建立應用程式服務,並部署使用服務主體與應用程式組態互動的範例應用程式。
# Change directory to the service principal sample
cd serviceconnector-webapp-appconfig-dotnet\service-principal
# Create a web app
LOCATION='eastus'
RESOURCE_GROUP_NAME='service-connector-tutorial-rg'
APP_SERVICE_NAME='webapp-appconfig-sp'
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-Z、0-9 和 -。 Azure 會附加隨機字串,讓伺服器端點在所有 Azure 中是唯一的。 |
webapp-appconfig-sp |
建立服務主體。 請務必將yourSubscriptionID替換為您的實際訂閱帳戶 ID。 將輸出儲存到臨時記事本。
az ad sp create-for-rbac --name myServicePrincipal --role Contributor --scopes /subscriptions/{yourSubscriptionID}/resourceGroups/$RESOURCE_GROUP_NAME
Warning
Microsoft 建議使用最安全的可用驗證流程。 此程序中所述的驗證流程需要對應用程式非常高度的信任。 它具有在其他流程中不存在的風險。 當其他更安全的流程 (例如受控識別) 無法運作時,才應該只使用此流程。
建立應用程式服務,並部署使用連接字串與應用程式組態互動的範例應用程式。
# Change directory to the service principal sample
cd serviceconnector-webapp-appconfig-dotnet\connection-string
# Create a web app
LOCATION='eastus'
RESOURCE_GROUP_NAME='service-connector-tutorial-rg'
APP_SERVICE_NAME='webapp-appconfig-cs'
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-Z、0-9 和 -。 Azure 會附加隨機字串,讓伺服器端點在所有 Azure 中是唯一的。 |
webapp-appconfig-cs |
建立 Azure 應用程式組態存放區。
APP_CONFIG_NAME='my-app-config'
az appconfig create --resource-group $RESOURCE_GROUP_NAME --name $APP_CONFIG_NAME --sku Free --location eastus
將測試組態檔匯入 Azure 應用程式組態。
使用系統指派的受控識別,將測試組態檔匯入 Azure 應用程式組態。
將目錄變更為資料夾 ServiceConnectorSample。
將 ./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
使用使用者指派的受控識別,將測試組態檔匯入 Azure 應用程式組態。
將目錄變更為資料夾 ServiceConnectorSample。
將 ./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
使用服務主體將測試組態檔匯入 Azure 應用程式組態。
將目錄變更為資料夾 ServiceConnectorSample。
將 ./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
Warning
Microsoft 建議使用最安全的可用驗證流程。 此程序中所述的驗證流程需要對應用程式非常高度的信任。 它具有在其他流程中不存在的風險。 當其他更安全的流程 (例如受控識別) 無法運作時,才應該只使用此流程。
使用連接字串將測試組態檔匯入 Azure 應用程式組態。
將目錄變更為資料夾 ServiceConnectorSample。
將 ./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 應用程式和應用程式組態存放區之間建立連線。 此連線會透過服務連接器來完成。
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_umi" --user-identity client-id=<myIdentityClientId> subs-id=<myTestSubsId>
user-identity 是指使用者指派的受控識別驗證類型。 服務連接器也支援下列驗證:系統指派的受控識別、連接字串(秘密)和服務主體。
有兩種方式可以找到 client-id:
- 在 Azure CLI 中,輸入
az identity show --name "myIdentity" --resource-group $RESOURCE_GROUP_NAME --query 'clientId'。
- 在 Azure 入口網站,打開先前建立的管理身份,並在 概覽中取得 客戶端 ID 下的值。
使用服務主體,在 Web 應用程式和應用程式組態存放區之間建立連線。 此設定是透過 Service Connector 完成。
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_sp" --service-principal client-id=<mySPClientId> secret=<mySPSecret>
service-principal 是指服務主體驗證類型。 服務連接器也支援下列驗證:系統指派的受控識別 (UMI)、使用者指派的受控識別 (UMI) 和連接字串 (秘密)。
使用連接字串,在 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_cs" --secret
secret 是指連接字串驗證類型。 服務連接器也支援下列驗證:系統指派的受控識別、使用者指派的受控識別和服務主體。
驗證連線
若要檢查連線是否正常運作,請流覽至您的 Web 應用程式。 若要進入 Web 應用程式,最簡單的方式是在 Azure 入口網站中開啟它。 在 概覽 頁面,選擇 預設網域。 網站啟動之後,您會看到它顯示: Hello。您的 Azure WebApp 現在已連線到 ServiceConnector 的應用程式設定。
運作方式
瞭解每個驗證類型的服務連接器在幕後管理的內容。
服務連接器會為您管理連線組態:
- 設定 Web 應用程式的
AZURE_APPCONFIGURATION_ENDPOINT,讓應用程式存取它並取得應用程式組態端點。 取得 範例程式碼。
- 啟用 Web 應用程式的系統指派的受控驗證,並將授與應用程式組態資料讀取者角色,讓應用程式使用 Azure.Identity 的 DefaultAzureCredential 向應用程式組態進行驗證。 取得 範例程式碼。
服務連接器會為您管理連線組態:
- 設定網頁應用程式的
AZURE_APPCONFIGURATION_ENDPOINT, AZURE_APPCONFIGURATION_CLIENTID 讓應用程式能存取並取得應用程式設定端點的 程式碼;
- 啟用 Web 應用程式的使用者的受控驗證,並將授與應用程式組態資料讀取者角色,讓應用程式使用 Azure.Identity 的 DefaultAzureCredential 向應用程式組態進行驗證。 取得 範例程式碼。
服務連接器會為您管理連線組態:
- 設定 Web 應用程式的
AZURE_APPCONFIGURATION_ENDPOINT,讓應用程式存取它並取得應用程式組態端點。 取得 範例程式碼。
- 將服務主體認證儲存至 WebApp AppSettings
AZURE_APPCONFIGURATION_CLIENTID。
AZURE_APPCONFIGURATION_TENANTID、AZURE_APPCONFIGURATION_CLIENTSECRET 並將 App Configuration Data Reader 角色授予服務主體,以便應用程式可以在 程式碼 中使用 ClientSecretCredential (來自 Azure.Identity) 驗證 App Configuration。
服務連接器會為您管理連線組態:
- 設定 Web 應用程式的
AZURE_APPCONFIGURATION_CONNECTIONSTRING,讓應用程式存取它並取得應用程式組態連接字串。 取得 範例程式碼。
- 啟用 Web 應用程式的系統指派的受控驗證,並將授與應用程式組態資料讀取者角色,讓應用程式使用 Azure.Identity 的 DefaultAzureCredential 向應用程式組態進行驗證。 取得 範例程式碼。
如需詳細資訊,請參閱 Service Connector 內部結構。
測試(可選的)
或者,執行下列測試:
更新應用程式組態存放區中金鑰 SampleApplication:Settings:Messages 的值。
az appconfig kv set --name <myAppConfigStoreName> --key SampleApplication:Settings:Messages --value hello --yes
流覽至您的 Azure Web 應用程式,如先前所述,然後重新整理頁面。 你看到訊息更新為: 你好。
清除資源
完成之後,如果您不會再使用這些 Azure 資源,請執行 az group delete 命令來刪除。 此命令會刪除您的資源群組及其中的所有資源。
az group delete --name <myResourceGroupName> --yes
後續步驟