你当前正在访问 Microsoft Azure Global Edition 技术文档网站。 如果需要访问由世纪互联运营的 Microsoft Azure 中国技术文档网站,请访问 https://docs.azure.cn

教程:使用服务连接器将 Web 应用连接到 Azure 应用程序配置

了解如何使用以下方法之一将 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 资源。

  1. 克隆以下示例存储库:

    git clone https://github.com/Azure-Samples/serviceconnector-webapp-appconfig-dotnet.git
    
  2. 将 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-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. 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 应用。 访问 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.

有关详细信息,请参阅服务连接器内部机制。

Test (optional)

(可选)执行以下测试:

  1. 更新应用程序配置存储中键 SampleApplication:Settings:Messages 的值。

    az appconfig kv set --name <myAppConfigStoreName> --key SampleApplication:Settings:Messages --value hello --yes
    
  2. 如前所述导航到 Azure Web 应用并刷新页面。 You see that the message is updated to: hello.

清理资源

完成后,如果不再使用这些 Azure 资源,请运行以下命令 az group delete 将其删除。 此命令将删除资源组及其中的所有资源。

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

Next step