创建自签名证书并将其上传到 Azure 应用服务的最简单方法

Easiest way to create and upload a self-signed certificate to Azure App Service

我正在寻找一些 CLI 命令或某种脚本,我可以执行它们以一步完成以下操作

  1. 创建自签名证书
  2. 将其上传到我的 Azure 应用服务
  3. 使用证书指纹作为值创建一个应用程序设置参数

以前有人做过吗?

1. Create a self-signed certificate

如果要创建自签名证书,我们可以使用OpenSSL to implement it. For more details, please refer to here and here

一个。创建证书密钥和签名 (csr)。

openssl req -new -x509 \
            -newkey rsa:2048 \            
            -sha256 \
            -days 3650 \
            -nodes \
            -out example.crt \
            -keyout example.key \
            -subj "/C=SI/ST=Ljubljana/L=Ljubljana/O=Security/OU=IT Department/CN=www.example.com"

下面列出了 -subj 行中指定的字段:

  • C= - 国家名称。两个字母的 ISO 缩写。
  • ST= - 州名或省名。
  • L= - 地区名称。您所在城市的名称。
  • O= - 您所在组织的全名。
  • OU= - 组织单位。
  • CN= - 完全限定的域名

b。生成证书

openssl pkcs12 \
            -inkey example.key \
            -in example.crt \
            -export -out example.pfx \
            -password pass:<your password>

2. Upload it to my Azure App Service and save the certificate thumbprint in Azure App service app settings

问题请参考以下代码

az login

# Upload the SSL certificate and get the thumbprint.
thumbprint=$(az webapp config ssl upload --certificate-file $pfxPath \
--certificate-password $pfxPassword --name $webappname --resource-group $resourceGroup \
--query thumbprint --output tsv)

# save thumbprint 
az webapp config appsettings set --name $appName --resource-group myResourceGroup \
  --settings "Certificate _Thumbprint=$thumbprint"