了解如何使用以下方法之一将 Azure 应用服务上运行的 ASP.NET Core 应用连接到 Azure 应用程序配置:
- 系统分配的托管标识 (SMI)
- 用户分配的托管标识 (UMI)
- Service principal
- Connection string
本教程使用 Azure CLI 完成以下任务:
- 设置 Azure 资源
- 在 Web 应用与应用程序配置之间创建连接
- 生成应用并将其部署到 Azure 应用服务
Prerequisites
登录 Azure
在 Azure CLI 中运行 az login 以登录到 Azure。
设置 Azure 资源
首先创建 Azure 资源。
克隆以下示例存储库:
git clone https://github.com/Azure-Samples/serviceconnector-webapp-appconfig-dotnet.git
将 Web 应用部署到 Azure。
按照以下步骤创建应用服务并部署示例应用。 Make sure you have the subscription Contributor or Owner role.
创建应用服务,并部署使用系统分配的托管标识来与应用程序配置交互的示例应用。
# 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 |
| 应用服务名称 |
应用服务名称用作 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 |
| 应用服务名称 |
应用服务名称用作 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 |
| 应用服务名称 |
应用服务名称用作 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 |
| 应用服务名称 |
应用服务名称用作 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。
Import the ./sampleconfigs.json test configuration file into the App Configuration store. If you're using Cloud Shell, upload sampleconfigs.json before you run the command.
az appconfig kv import --name $APP_CONFIG_NAME --source file --format json --path ./sampleconfigs.json --separator : --yes
使用用户分配的托管标识将测试配置文件导入 Azure 应用程序配置。
将目录更改为文件夹 ServiceConnectorSample。
Import the ./sampleconfigs.json test configuration file into the App Configuration store. If you're using Cloud Shell, upload sampleconfigs.json before you run the command.
az appconfig kv import --name $APP_CONFIG_NAME --source file --format json --path ./sampleconfigs.json --separator : --yes
使用服务主体将测试配置文件导入 Azure 应用程序配置。
将目录更改为文件夹 ServiceConnectorSample。
Import the ./sampleconfigs.json test configuration file into the App Configuration store. If you're using Cloud Shell, upload sampleconfigs.json before you run the command.
az appconfig kv import --name $APP_CONFIG_NAME --source file --format json --path ./sampleconfigs.json --separator : --yes
Warning
Microsoft 建议使用最安全的可用身份验证流。 此过程中描述的身份验证流要求对应用程序拥有极高程度的信任。 它具有在其他流中不存在的风险。 请仅在无法使用其他更安全的流(例如托管标识)时才使用此流。
使用连接字符串将测试配置文件导入 Azure 应用程序配置。
将目录更改为文件夹 ServiceConnectorSample。
Import the ./sampleconfigs.json test configuration file into the App Configuration store. If you're using Cloud Shell, upload sampleconfigs.json before you run the command.
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'。
- In the Azure portal, open the Managed Identity that was created earlier and in Overview, get the value under Client ID.
使用服务主体在 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_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 连接到应用配置。 After the website is up, you see it display: Hello. Your Azure WebApp is connected to App Configuration by ServiceConnector now.
工作原理
了解服务连接器在幕后为每种身份验证类型管理哪些配置。
服务连接器为你管理连接配置:
- 设置 Web 应用的
AZURE_APPCONFIGURATION_ENDPOINT,以允许应用程序访问它并获取应用程序配置终结点。 Access sample code.
- 激活 Web 应用的系统分配的托管身份验证,并为应用程序配置授予数据读取者角色,以允许应用程序使用 Azure.Identity 中的 DefaultAzureCredential 向应用程序配置进行身份验证。 Access sample code.
服务连接器为你管理连接配置:
- Set up the web app's
AZURE_APPCONFIGURATION_ENDPOINT, AZURE_APPCONFIGURATION_CLIENTID to let the application access it and get app configuration endpoint in code;
- 激活 Web 应用的用户分配的托管身份验证,并为应用程序配置授予数据读取者角色,以允许应用程序使用 Azure.Identity 中的 DefaultAzureCredential 向应用程序配置进行身份验证。 Access sample code.
服务连接器为你管理连接配置:
- 设置 Web 应用的
AZURE_APPCONFIGURATION_ENDPOINT,以允许应用程序访问它并获取应用程序配置终结点。 Access sample code.
- 将服务主体凭据保存到 WebApp 的 AppSettings 中
AZURE_APPCONFIGURATION_CLIENTID。 AZURE_APPCONFIGURATION_TENANTID, AZURE_APPCONFIGURATION_CLIENTSECRET and grant App Configuration Data Reader role to the service principal, so the application could be authenticated to the App Configuration in code, by using ClientSecretCredential from Azure.Identity.
服务连接器为你管理连接配置:
- 设置 Web 应用的
AZURE_APPCONFIGURATION_CONNECTIONSTRING,以允许应用程序访问它并获取应用程序配置连接字符串。 Access sample code.
- 激活 Web 应用的系统分配的托管身份验证,并为应用程序配置授予数据读取者角色,以允许应用程序使用 Azure.Identity 中的 DefaultAzureCredential 向应用程序配置进行身份验证。 Access sample code.
有关详细信息,请参阅服务连接器内部机制。
Test (optional)
(可选)执行以下测试:
更新应用程序配置存储中键 SampleApplication:Settings:Messages 的值。
az appconfig kv set --name <myAppConfigStoreName> --key SampleApplication:Settings:Messages --value hello --yes
如前所述导航到 Azure Web 应用并刷新页面。 You see that the message is updated to: hello.
清理资源
完成后,如果不再使用这些 Azure 资源,请运行以下命令 az group delete 将其删除。 此命令将删除资源组及其中的所有资源。
az group delete --name <myResourceGroupName> --yes
Next step