首页 > 解决方案 > 如何将私钥证书 (.pfx)、公钥证书 (.cer) 上传到 Azure WebApp

问题描述

如何使用 Azure Powershell 将私有、公共证书上传到 Azure AppService。我知道 New-AzureRmWebAppSSLBinding 但我没有进行任何 SSL 绑定。

我们有使用 SSL 绑定的 Azure 应用服务。为此,我使用 New-AzureRmWebAppSSLBinding 上传证书。我确实在我的网络应用程序上为每个主机上传了一个证书。这工作正常。但我想将额外的私有和公共证书上传到此应用服务以进行 API 验证。我没有找到任何 azure powershell 命令来上传私有或公共证书。

Azure 门户允许上传私有证书及其密码或公共证书。但是我想使用 powershell 做同样的事情。门户 UI 还具有从密钥库导入证书的选项。我确定可以将证书上传到密钥保管库,但没有 powershell 命令将其导入 Azure 应用服务。

<a href="https://ibb.co/Kh7t5DL"><img src="https://i.ibb.co/fFt3X9n/Capture-Cert.jpg" alt="Capture-Cert" border="0"></a>

我已经阅读了这些文章,但它们都使用相同的命令。 https://github.com/Azure/azure-powershell/issues/2108 如何使用 Powershell 向 Azure RM 网站添加证书

New-AzureRmWebAppSSLBinding -ResourceGroupName $RGName -WebAppName $webAppName -CertificateFilePath $filePath -CertificatePassword $pass

如果我调用此方法,它会询问主机名。由于我已经为此主机名上传了带有 SSL 绑定的证书,因此我无法使用它。如果不提供主机名,此命令将失败。

标签: azurepowershellsslcertificateazure-web-app-service

解决方案


好的,最后我能够弄清楚并上传私人和公共证书。Azure 资源浏览器对了解文件夹结构和证书位置非常有帮助。

上传公共证书:这些是每个应用服务附加的。

$webApps = @{
            "Dev_AppServicesGroup" = "DevUserService"
        }
$certName = "chain-cert.cer"
$Path = "C:\Certs"    

$fullpath = $path + '\' + $certname
$pwd = ConvertTo-SecureString -String 'anyPwd' -AsPlainText -Force
$cert  = New-AzureRmApplicationGatewaySslCertificate -Name 'someCert' -CertificateFile $fullpath -Password $pwd
$apiVersion = '2018-02-01'

if($cert)
{
    $PropertiesObject = @{
        blob=$cert.Data; 
        publicCertificateLocation= "CurrentUserMy"
    }

    foreach($resourceGroup in $webApps.Keys)
    {
       $webAppName = $webApps.Item($resourceGroup)        
       $resource = Get-AzureRmWebApp -ResourceGroupName $resourceGroup -Name $webAppName
       $resourceName = $resource.Name + "/"+$certName
       New-AzureRmResource -Location $resource.Location -PropertyObject $PropertiesObject -ResourceGroupName $resource.ResourceGroup -ResourceType Microsoft.Web/sites/publicCertificates -ResourceName $resourceName -ApiVersion $apiVersion -Force        

       #Apply the cert to the deployment slots if any
       $slots = Get-AzureRmResource -ResourceGroupName $resource.ResourceGroup -ResourceType Microsoft.Web/sites/slots -ResourceName $webAppName -ApiVersion $apiVersion
       foreach($slot in $slots)
       {            
          $resourceName = $slot.Name + "/"+$certName                     
          New-AzureRmResource -Location $slot.Location -PropertyObject $PropertiesObject -ResourceGroupName $slot.ResourceGroupName -ResourceType Microsoft.Web/sites/slots/publicCertificates -ResourceName $resourceName -ApiVersion $apiVersion -Force            
       }
    }
}

上传私有证书:这些证书是按资源组上传的,可供该组下的所有应用服务使用。

#Private certs needs to be uploaded to each resource group with app services
$resourceGroups = @("Dev_AppServicesGroup1", "Dev_AppServicesGroup2")
$certName = "event-store-user.p12"

$certPwd = "Your certificate password" #This is the private cert password
$Path = "C:\Certs"   

$fullpath = $path + '\' + $certname    

$pwd = ConvertTo-SecureString -String 'SomePwd' -AsPlainText -Force
$cert  = New-AzureRmApplicationGatewaySslCertificate -Name someCert -CertificateFile $fullpath -Password $pwd
$apiVersion = '2018-02-01'

if($cert)
{
    $PropertiesObject = @{
        pfxBlob=$cert.Data;  
        password =$certPwd; #This is the private cert password        
        ResourceType = "Microsoft.Web/Certificates"
    }

    foreach($resourceGroup in $resourceGroups)
    {
        $resource = Get-AzureRmResourceGroup -Name $resourceGroup       
        New-AzureRmResource -ResourceName $certName -Location $resource.Location -PropertyObject $PropertiesObject -ResourceGroupName $resource.ResourceGroupName -ResourceType Microsoft.Web/certificates -ApiVersion $apiVersion -Force        
    }
}

而已。要上传 SSL 证书并将其绑定到应用服务,您可以使用命令“New-AzWebAppSSLBinding”。


推荐阅读