首页 > 解决方案 > 如何使用 Powershell 在 Azure API 管理中禁用订阅密钥

问题描述

我们正在使用 Azure API 管理和 Powershell 通过脚本在门户上导入配置,但是每当导入 API 时,都会启用来自门户的“需要订阅”检查(API 需要订阅密钥)。我们现在没有在 API 上使用订阅功能,所以我们需要在导入时禁用它。我们正在使用Import-AzApiManagementApiSet-AzApiManagementApi使用以下代码:

Set-AzApiManagementApi -ApiId $apiId -Context $context -Protocols @('https') -ServiceUrl $serviceBase$path -Name $api.Name
Set-AzApiManagementPolicy -Context $context -ApiId $apiId -PolicyFilePath "$pwd/src/private/security_policy.xml"

我们没有在文档中找到不进行此检查的方式来导入 API。是否有任何脚本可以通过 powershell 禁用此功能?

标签: azurepowershellazure-api-management

解决方案


快速补充这一点。以下 Powershell 命令将 API 的 Subscription Required 属性设置为 false。不过要提醒一句:这目前不适用于 Azure DevOps 管道。管道将声称任务已成功运行,如果您在管道中运行 $api 的回显,它将显示该 api 具有 SubscriptionRequired : False。但是,如果你去检查 Azure 中的 api,它仍然会设置为 True(默认行为)。

# get context
$apimContext = New-AzApiManagementContext -ResourceGroupName "targetResourceGroup" -ServiceName "targetApimInstance"
$api = Get-AzApiManagementApi -Context $apimContext -Name "Name of Api"

# set subscriptionRequired to false
$api.SubscriptionRequired=$false

推荐阅读