You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Bicep中获取Bot Service的DirectLine密钥并配置到应用服务

在Bicep中直接获取Bot Service的DirectLine密钥并配置到应用服务

核心方法:使用listKeys函数

Bot Service的DirectLine密钥可以通过Bicep内置的listKeys函数直接获取,无需额外CLI或HTTP调用。这个函数能检索资源的访问密钥,适用于支持密钥管理的Azure资源,包括Bot Service。

具体实现步骤

  1. 定义Bot Service资源
    先在Bicep中定义Bot Service资源,若需自动启用DirectLine通道,可在配置中添加对应通道设置:

    resource botService 'Microsoft.BotService/botServices@2022-09-15' = {
      name: 'myBotService'
      location: 'eastus'
      sku: {
        name: 'F0'
      }
      properties: {
        displayName: 'My Bot'
        endpoint: 'https://${appService.name}.azurewebsites.net/api/messages'
        msaAppId: '${azureADApp.applicationId}'
        // 自动启用DirectLine通道
        channels: [
          {
            name: 'DirectLineChannel'
            properties: {
              isEnabled: true
            }
          }
        ]
      }
    }
    
  2. 获取DirectLine密钥
    通过listKeys函数获取Bot的密钥集合,再筛选出类型为DirectLine的密钥值:

    var directLineKey = (listKeys(botService.id, botService.apiVersion).properties.keys).filter(key => key.keyType == 'DirectLine')[0].value
    
  3. 配置应用服务的应用设置
    将获取到的DirectLine密钥添加到应用服务的应用设置中:

    resource appService 'Microsoft.Web/sites@2022-03-01' = {
      name: 'myAppService'
      location: 'eastus'
      sku: {
        name: 'B1'
      }
      properties: {
        serverFarmId: appServicePlan.id
        siteConfig: {
          appSettings: [
            {
              name: 'DIRECT_LINE_KEY'
              value: directLineKey
            }
            // 其他应用设置
          ]
        }
      }
    }
    

注意事项

  • 权限要求:部署模板的身份需拥有Bot Service的Microsoft.BotService/botServices/listKeys/action权限,才能成功调用listKeys函数。
  • 密钥同步:若后续手动轮换Bot的DirectLine密钥,应用服务的设置不会自动同步,需重新部署模板或手动更新。

内容的提问来源于stack exchange,提问作者Don Chambers

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.17 23:42:11