订阅转移后无法导入应用服务证书

Unable to import App Service Certificate after Subscription transfer

我最近将 Azure 订阅转移到新目录中的新帐户所有者。这样做之后,我无法再将应用服务证书导入我的应用服务,因此无法创建任何 TSL/SSL 绑定

当我单击导入应用程序服务证书时,证书正确显示;当我单击证书时,它会正确显示 Validated the App Service Certificate;但是当我单击 确定 时,出现以下错误:

Failed to add App Service certificate to the app, Check error for more details. Error Details: The service does not have access to 'MyKeyVault' Key Vault. Please make sure that you have granted necessary permissions to the service to perform the request operation.

此外,如果我打开我的证书密钥库,然后单击 KeysSecretsCertificates ,我收到消息:

Something went wrong. Please refresh and try again.

最后,在证书库的 Activity 日志中,我可以看到我在迁移时触发了一个 更新密钥库 事件,所以我我假设这可能是相关的,但从 Activity 日志 什么 中不清楚该事件是什么。

请注意,我的帐户已分配给订阅的 所有者 角色。

我的问题: 在这些条件下,如何将我的应用服务证书导入应用服务?或者,如何解决这些问题以进一步隔离此问题?

预计在将订阅转移到新目录后您将无法导入您的应用服务证书——尽管您应该 在您的 Key Vault 中收到以下警告:

The directory (XYZ) currently selected differs from this key vault's directory. Some actions will be disabled. Click this for more details.

发生这种情况时,您通常会也会在 Key Vault 的 KeysSecrets 中收到以下错误消息、and/or 证书 标签:

The operation ‘List’ is not enabled in this key vault's access policy.

这些消息并不总是立即显示,可能是由于依赖项在不同数据中心之间迁移,这可能是您收到信息量较少的错误的原因。我的假设是您看到的通用消息会及时被这些特定消息替换,但它们都源于相同的潜在问题。

将您的 Key Vault 与您的新目录相关联

无论如何,确保您的 Key Vault 实际上在订阅转移后与新目录相关联是个好主意。根据 the instructions linked from the Azure error, you can change the directory in the Azure PowerShell (Az) module 使用以下命令:

Select-AzSubscription -SubscriptionId <your-subscriptionId>
$vaultResourceId = (Get-AzKeyVault -VaultName myvault).ResourceId
$vault = Get-AzResource –ResourceId $vaultResourceId -ExpandProperties
$vault.Properties.TenantId = (Get-AzContext).Tenant.TenantId
$vault.Properties.AccessPolicies = @()
Set-AzResource -ResourceId $vaultResourceId -Properties $vault.Properties

或者,请参阅 Stack Overflow 上的“”,其中提供了类似的说明。

Note: It's useful to have Azure PowerShell (Az) module installed. If you don't, however, you can always use the Azure Cloud Shell, which is accessible in the Azure Portal via the shell icon to the right of the search bar. In that case, you may need to provision an Azure Storage account, if you haven't already.

验证分辨率

上述步骤应该 可以解决您的问题。但是您应该检查两个相关条件以确保安全。

应用服务证书

首先,转到门户中的应用服务证书。您的 App Service 证书和您的 Key Vault 之间应该有一个 link,这允许您的私钥存储在您的 Key Vault 中,并由具有适当访问策略的应用程序检索。 Azure 现在有内置工具来检测应用服务证书何时未正确 linked 到您的 Key Vault。要访问它,请单击 证书配置 ,并确保勾选 第 1 步:存储 旁边的复选标记。如果不是这种情况,门户网站会给您一个警告,您可以单击该警告自动重新建立此 link。

Key Vault 访问策略

其次,return 到您的 Key Vault,然后单击 KeysSecrets证书。您不应再在此处收到任何错误。但是,如果这样做,您可能需要重新配置或更新您的 Microsoft Azure App Service 访问策略,该策略通常在您首次创建Azure 应用服务证书。

Note: There are a lot of previous posts for troubleshooting and resolving access to the App Services via an Access Policy (e.g., , but also on Microsoft Questions), so I'm not going to go into detail on those here. But I want to acknowledge that particular possibility just in case the App Service Certificate isn't properly registered after the Subscription migration.

完成这一切后,您现在应该可以在您的应用服务中使用导入应用服务证书

我使用的是 Azure App Certificates,在订阅移动后,证书的自动续订失败,尽管已将 Key Vault 移动到新租户,Change a Key Vault tenant ID after a subscription move

进入我的 App Services 并点击 Import App Service Certificate 让我选择最新的证书并验证它,但随后出现以下错误:

Failed to add App Service certificate to the app, Check error for more details. Error Details: The parameter KeyVaultId & KeyVaultSecretName has an invalid value.

在我的例子中,解决方案是删除应用服务证书的所有绑定和所有实例(即删除 microsoft.web/certificates 而不是 应用服务证书本身),然后重新绑定到最新的证书。