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

Azure Bicep部署应用服务槽未添加用户托管身份求助

问题根源

当使用cloningInfo克隆现有Web应用槽位时,Azure会默认复制源应用的身份配置,这会直接覆盖你在模板中为新槽位指定的用户托管身份设置,导致部署完成后槽位未关联目标身份。

解决方案

将槽位部署拆分为两个步骤:先完成克隆创建槽位,再单独更新槽位的身份配置,确保身份设置不会被克隆操作覆盖。

修改后的Bicep模板

@description('Name of the api to add the slot.')
param apiName string

@description('Name of the slot.')
param slotName string

@description('Name of the location of the existing api.')
param locationName string = resourceGroup().location

@description('Name of the managed identity to assign to the slot.')
param managedIdentityName string

// 统一API版本为最新稳定版,避免版本差异问题
resource apiParent 'Microsoft.Web/sites@2022-03-01' existing = {
  name: apiName
}

resource uami 'Microsoft.ManagedIdentity/userAssignedIdentities@2023-01-31' existing = {
  name: managedIdentityName
}

output uamiId string = uami.id

// 第一步:克隆创建槽位,暂不设置身份
resource apiDeploymentSlot 'Microsoft.Web/sites/slots@2022-03-01' = {
  name: slotName
  parent: apiParent
  location: locationName
  properties: {
    serverFarmId: apiParent.properties.serverFarmId
    cloningInfo: {
      sourceWebAppId: apiParent.id
    }
  }
}

// 第二步:更新槽位的身份配置,依赖于第一步的克隆完成
resource apiDeploymentSlotIdentity 'Microsoft.Web/sites/slots@2022-03-01' = {
  name: slotName
  parent: apiParent
  location: locationName
  identity: {
    type: 'UserAssigned'
    userAssignedIdentities: {
      '${uami.id}': {}
    }
  }
  properties: {
    keyVaultReferenceIdentity: uami.id
  }
  dependsOn: [apiDeploymentSlot]
}

关键说明

  1. 统一API版本:将所有资源的API版本统一为2022-03-01(Web应用)和2023-01-31(托管身份),避免跨版本配置兼容性问题。
  2. 拆分部署步骤:先完成克隆创建槽位,再通过独立的资源块设置身份,利用dependsOn确保克隆完成后再执行身份配置更新,避免被克隆操作覆盖。
  3. 明确身份关联:同时设置identity(槽位的托管身份)和keyVaultReferenceIdentity(Key Vault引用专用身份),确保两者指向同一个用户托管身份。

验证步骤

部署完成后,可通过以下方式验证:

  • 进入Azure门户,查看目标槽位的身份设置,确认已关联指定的用户托管身份。
  • 测试槽位访问Key Vault密钥的权限,验证身份配置生效。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.29 08:33:27