Add a certificate to an Azure WebApp and reference it in code - azure

I have an web application on IIS. The asp.net application call external resources and need to send a certificate for authorization. I cannot use the certificate store on Azure in a Web App. How can I store a certificate in Azure and reference it when I make a external call?

In order to use certificated you first have to upload the certifcate to your webapp. You can do this to go to the setting of your webapp and goto custom domain and SSL. Here you can upload your certificate.
In order to be usable in your application Azure cannot use the certificate store but loads the certificates into memory. You can do this by adding WEBSITE_LOAD_CERTIFICATES with value * or you can do it via PowerShell via:
$appSettings = "WEBSITE_LOAD_CERTIFICATES" = "*"}
$w = Set-AzureWebsite $siteName -AppSettings $appSettings
More info can be found at https://azure.microsoft.com/en-us/blog/using-certificates-in-azure-websites-applications/

Related

Azure - App Service Certificate in Key Vault is blank - Unable to download the pfx using powershell / az cli

I have created an Azure App Service Certificate - Wildcard and have linked it to an Azure Key Vault, also done with Domain Verification. Now I want to export this certificate as PFX and use in other components.
However, If I go to the Key Vault secret it shows up as blank.
I have tried running powershell scripts to export/download the cert but doesn't work.
The powershell script to export the cert from Key Vault is taken from https://azure.github.io/AppService/2017/02/24/Creating-a-local-PFX-copy-of-App-Service-Certificate.html
As this used the retired AzureRM module, I found a more up to date code which uses Azure Powershell scripts # https://raw.githubusercontent.com/Anitalex/poshscripts/a7e6c8153ab9f9979792eb8c07497cd42e39778d/azure/ExportWebAppCertificate.ps1
But similar set of steps..
I have tried Re-Keying the certificate in the App Service Certificate and the problem persists ?
Argh...
Here is the solution which doesn't seem to be documented anywhere..
I don't know if it's the only way, but looks like it - We have to
Create an Azure WebApp ( or an AppService)
Turn on TLS/SSL
Set an Identity - either System Assigned/User Assigned
Grant this Identity required permission to the KeyVault [ won't work with the Azure role-based access control (preview) ]
Go to the tab "Private Key Certificates (.pfx)"
Use the "Import App Service Certificate" - you will need to select your cert from the dropdowns.
Once the certificate is successfully imported, the pfx will be populated in the Key Vault certificate and now you can download.

Retrieve/recover cert pfx from Azure Application Gateway

I want to retrieve/download the original pfx certificate that was uploaded onto our Application Gateway.
So far I have done the following in PowerShell:
$appGw = Get-AzApplicationGateway -Name "appgatewayname" -ResourceGroupName "appgatewayresourcegroup"
$certs = Get-AzApplicationGatewaySslCertificate -ApplicationGateway $appGw
$certBase64 = $certs[0].PublicCertData
$certBytes = [System.Convert]::FromBase64String($certBase64)
[io.file]::WriteAllBytes("C:\directory\newcert.pfx", $certBytes)
However, it doesn't seem to be a valid certificate, especially when I try to upload it onto our KeyVault.
Is anyone familiar, and is this even possible? Should I use the Data property instead of the PublicCertData?
.pfx file can't be retrieved after you uploaded it to an application gateway listener. I have checked Azure resource explorer, there is no API provided by Azure to retrieve it. And I also double-checked with the Azure product team.
Though seems we can use Get-AzApplicationGatewaySslCertificate PS command to get certs, but actually, it provides you with PublicCertData only, there is no PrivateCertData of .pfx file. I also have tested on my side, the value of Data property is empty.
So if you want to upload this .pfx file to Azure KV, I am afraid the only way is uploading the original pfx certificate to Azure KV directly.

Bulk/mass change SSL certificates on Azure app services (web apps)

I've a wildcard SSL that has expired, and I have the new cert (different authority) uploaded to Azure already..
..but I'd like to know if there's a way to bulk change all the sites using the old cert, over to the new cert. On normal IIS when you make a change for any one of your sites on the old cert, to the new cert, it asks you if you want to update other bindings that are using the old cert so that they also use the new cert. I've around 30 sites I need to move to the new cert and it's going to be quite a drag one by one
Is there an equivalent functionality on Azure? Powershell is acceptable if the portal.azure.com won't do it..
I've been using Azure CLI in Powershell for this. First build a CSV file or array with the values. It will need to contain:
- Web App Name
$Thumbprint
Then iterate through
$Thumbprint = "12bnhgikjbkj13kjbblahblah"
$WebApps = #("WebApp1","WebApp2","WebApp3") #OR
Foreach ($WebApp in $WebApps) {
az webapp config ssl bind --certificate-thumbprint $Thumbprint --ssl-type SNI --resource-group ResourceGroupName --name "$WebApp"
}
You can also do it with New-AzureRmWebAppSSLBinding from here. Also a guide here on how to do it.
I used the following code in Azure Cloud Shell to enumerate how many apps were using the old thumbprint and updated them to the new one. I found this easier than writing down each of the app service names.
You'll need to make sure you import your new certificate first.
az account set --subscription "My Application"
$webapp_ids=(az webapp list --query "[?hostNameSslStates.[thumbprint=='OLD_THUMBPRINT']].id" --output tsv)
az webapp config ssl bind --certificate-thumbprint "NEW_THUMBPRINT" --ssl-type SNI --ids $webapp_ids

Upload Certificate to App Service from key Vault

In Azure Key Vault Certificate will be auto renewed nearer to expiry date.
would like to know how can renewed certificate be uploaded to App-Service/ Azure Functions.
Is there a hook available on KeyVault to listen for Certificate successful recreation. so Thumbprint and renewed certificate will be updated to App Service.
No, there is no such hook. You can use azure powershell\cli\rest api\whatever to retrieve the certificate and push it to the app service. You can configure Azure Function\Runbook to do that on a schedule, so you dont have to worry about it.
Rest Api: https://learn.microsoft.com/en-us/rest/api/appservice/certificates/createorupdate
Powershell: https://learn.microsoft.com/en-us/azure/app-service/scripts/powershell-configure-ssl-certificate
Azure Cli: https://learn.microsoft.com/en-us/azure/app-service/scripts/cli-configure-ssl-certificate

Can you delete Microsoft.Web/certificates?

I'm creating LetsEncrypt SSL certificates for each of our Web App Services, then binding it to the Web App and subsequently trying to delete the previous one from our Azure resources. I notice Remove-AzureRmWebAppSSLBinding has a -DeleteCertificate flag after unbinding an SSL certificate, but I'd prefer not to unbind/delete until a new certificate has been obtained an bound.
Is there any other way that I can remove the certificates from Azure?
I found that I can delete any resource by ID using Remove-AzureRmResource.
Remove-AzureRmResource -ResourceId $resourceId -Force
This solves my problem for now!

Resources