【Azure 云服务】为Azure云服务配置上自签名的SSL证书步骤

简介: 【Azure 云服务】为Azure云服务配置上自签名的SSL证书步骤

问题描述

在使用Azure Cloud Service(云服务),默认的情况下都是使用的 HTTP 服务,通过 Visual Studio 2022 创建的默认 Cloud Service项目中,在ServiceDefinition.csdef 服务定义文件中,值默认开启了HTTP 80的Endpoint。

<InputEndpoint name="Endpoint1" protocol="http" port="80" />

而如果要让云服务使用HTTPS,需要那些操作步骤呢? 在官网中,有两部分文档对此有所介绍:

第一部分:云服务证介绍和生成自签名证书 https://docs.azure.cn/zh-cn/cloud-services/cloud-services-certs-

create#create-a-new-self-signed-certificate
$cert = New-SelfSignedCertificate -DnsName yourdomain.chinacloudapp.cn -CertStoreLocation "cert:\LocalMachine\My" -KeyLength 2048 -KeySpec "KeyExchange"
$password = ConvertTo-SecureString -String "your-password" -Force -AsPlainText
Export-PfxCertificate -Cert $cert -FilePath ".\my-cert-file.pfx" -Password $password

第二部分:为云服务 配置TLS https://docs.azure.cn/zh-cn/cloud-services/cloud-services-configure-ssl-certificate-portal#step-2-modify-the-service-definition-and-configuration-files

参照以上两部分内容,就可以实现为云服务配置自签名证书。虽然通过浏览器访问时,还是会提示自签名证书不受信任,但在实验阶段已完全可用!

最终结果如下:

 

实现步骤

第一步: 通过Windows 机器使用Powershell脚本生成 以云服务域名为主题的自签名证书 (注意:需要以管理员权限运行PowerShell)

注意:如果没有使用管理员权限执行 New-SelfSignedCertificate 命令,则会出现权限不够的提示信息“New-SelfSignedCertificate : CertEnroll::CX509Enrollment::_CreateRequest: Access is denied. 0x80070005 (WIN32: 5 ERROR_ACCESS_DENIED)”

证书生成完毕后,进入C:\WINDOWS\system32 目录,找到 my-cert-file.pfx 文件,双击,安装此证书到本机。最后,通过 Certmgr 证书管理工具查看证书的指纹信息(thumbprint)

 

第二步:修改云服务配置文件,添加 Certificates , Endpoints 以及 Site Binding

参考“为Azure云服务配置SSL”文章中的第二部分,修改服务定义和配置文件。可以完全参考文档中的步骤2操作,本试验中,因为使用的是自签名证书,所以就没有有配置CA root部分。

一:修改 ServiceDefinition.csdef 文件中的 Certificates, InputEndpoint 和 Site Binding

<?xml version="1.0" encoding="utf-8"?>
<ServiceDefinition name="AzureCloudService1" xmlns="http://schemas.microsoft.com/ServiceHosting/2008/10/ServiceDefinition" schemaVersion="2015-04.2.6">
    <WebRole name="WebRole2" vmsize="Standard_D1_v2">
        <Sites>
            <Site name="Web">
                <Bindings>
                    <Binding name="Endpoint1" endpointName="Endpoint1" />
                    <Binding name="HttpsIn" endpointName="HttpsIn" />
                </Bindings>
            </Site>
        </Sites>
        <ConfigurationSettings>
            <Setting name="Microsoft.WindowsAzure.Plugins.Diagnostics.ConnectionString"/>
        </ConfigurationSettings>
        <Endpoints>
            <InputEndpoint name="Endpoint1" protocol="http" port="80" />
            <InputEndpoint name="HttpsIn" protocol="https" port="443"
             certificate="SampleCertificate" />
        </Endpoints>
        <Imports>
            <Import moduleName="RemoteAccess" />
            <Import moduleName="RemoteForwarder" />
        </Imports>
        <Certificates>
            <Certificate name="SampleCertificate"
                        storeLocation="LocalMachine"
                        storeName="My"
                        permissionLevel="limitedOrElevated" />
            <!-- IMPORTANT! Unless your certificate is either
        self-signed or signed directly by the CA root, you
        must include all the intermediate certificates
        here. You must list them here, even if they are
        not bound to any endpoints. Failing to list any of
        the intermediate certificates may cause hard-to-reproduce
        interoperability problems on some clients.-->
            <!--<Certificate name="CAForSampleCertificate"
                        storeLocation="LocalMachine"
                        storeName="CA"
                        permissionLevel="limitedOrElevated" />-->
        </Certificates>
    </WebRole>
</ServiceDefinition>
  1. 在 Sites 节中添加 Binding 元素。 此元素添加 HTTPS 绑定以将终结点映射到站点
  2. 在“Endpoints”部分中添加 InputEndpoint 元素以启用 HTTPS
  3. Certificates 节定义了证书的名称、位置及其所在存储的名称

二:在服务配置文件 (CSCFG) ServiceConfiguration.Cloud.cscfg 中,添加 Certificates 值并为其指定证书的指纹(thumbprint)

<?xml version="1.0" encoding="utf-8"?>
<ServiceConfiguration serviceName="AzureCloudService1" xmlns="http://schemas.microsoft.com/ServiceHosting/2008/10/ServiceConfiguration" osFamily="6" osVersion="*" schemaVersion="2015-04.2.6">
    <Role name="WebRole2">
        <Instances count="1" />
    
    ... ...
    
        <Certificates>
            <Certificate name="Microsoft.WindowsAzure.Plugins.RemoteAccess.PasswordEncryption" thumbprint="B8E0XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXD6D8" thumbprintAlgorithm="sha1" />
            <Certificate name="SampleCertificate"
              thumbprint="deb8bff5ced1e43e0723cdf9857b6a6ca1d793b2"
              thumbprintAlgorithm="sha1" />
        </Certificates>
    </Role>
</ServiceConfiguration>

在云服务项目文件中修改的动图说明如下:

 

第三步:上传第一步生成的 my-cert-file.pfx证书到 云服务的 Certificates 页面

在门户中,上传服务证书。 这一步需要在部署之前操作,否则会出现部署失败。失败原因为:The certificate with thumbprint deb8bff5ced1e43e0723cdf9857b6a6ca1d793b2 was not found.'

 

第四步:重新部署云服务, 然后使用https访问,而不是http

如果使用自签名证书,浏览到与自签名证书关联的 HTTPS 终结点时,浏览器中可能会显示一个证书错误。 使用由受信任证书颁发机构签名的证书可消除此问题;同时,你可以忽略此错误。 (也可以将自签名证书添加到用户的受信任证书颁发机构证书存储中。)

 

参考资料

云服务证介绍和生成自签名证书 : https://docs.azure.cn/zh-cn/cloud-services/cloud-services-certs-create#create-a-new-self-signed-certificate

为云服务 配置TLS : https://docs.azure.cn/zh-cn/cloud-services/cloud-services-configure-ssl-certificate-portal#step-2-modify-the-service-definition-and-configuration-files

 

相关文章
|
30天前
|
应用服务中间件 Linux 网络安全
如何在 CentOS 7 上为 Nginx 创建自签名 SSL 证书
如何在 CentOS 7 上为 Nginx 创建自签名 SSL 证书
97 1
|
20天前
|
安全 网络安全 Windows
【Azure App Service】遇见az命令访问HTTPS App Service 时遇见SSL证书问题,暂时跳过证书检查的办法
【Azure App Service】遇见az命令访问HTTPS App Service 时遇见SSL证书问题,暂时跳过证书检查的办法
【Azure App Service】遇见az命令访问HTTPS App Service 时遇见SSL证书问题,暂时跳过证书检查的办法
|
30天前
|
负载均衡 前端开发 应用服务中间件
使用Nginx配置SSL以及部署前端项目
本文介绍了如何使用Nginx配置SSL证书以启用HTTPS,并展示了如何通过Nginx部署前端项目,包括配置SSL证书、设置代理和负载均衡的示例。
68 2
|
20天前
|
存储 Linux 网络安全
【Azure App Service】.NET代码实验App Service应用中获取TLS/SSL 证书 (App Service Linux/Linux Container)
【Azure App Service】.NET代码实验App Service应用中获取TLS/SSL 证书 (App Service Linux/Linux Container)
|
20天前
|
网络安全 API 数据安全/隐私保护
【Azure App Service】.NET代码实验App Service应用中获取TLS/SSL 证书 (App Service Windows)
【Azure App Service】.NET代码实验App Service应用中获取TLS/SSL 证书 (App Service Windows)
|
30天前
|
缓存 Ubuntu 应用服务中间件
如何在 Ubuntu 14.04 上配置 Varnish Cache 4.0 实现 SSL 终止
如何在 Ubuntu 14.04 上配置 Varnish Cache 4.0 实现 SSL 终止
37 0
|
3月前
|
安全 网络安全
如何给网站添加ssl安全证书
如何给网站添加ssl安全证书
79 1
|
10天前
|
运维 安全 数据建模
阿里云免费SSL证书和收费版SSL证书有什么区别?
阿里云提供免费与收费SSL证书,前者有效期仅3个月,适合个人网站或测试使用;后者有效期至少1年,具备更高安全等级、良好兼容性及OCSP验证稳定性等优势,适用于企业网站,尤其政府、金融等领域建议选用OV或EV型证书以确保数据与身份认证安全。详细了解与报价请访问SSL证书官方页面。
102 2
|
10天前
|
网络协议 网络安全 数据安全/隐私保护
🔒SSL免费证书自动申请及续期-Certimate
Certimate 是一款旨在简化 SSL 证书管理的工具,尤其适用于需要维护多个域名的个人或小企业用户。它可以自动申请和续期证书,避免了手动操作的繁琐与易忘问题。Certimate 支持私有部署,确保数据安全,并能通过简单配置自动完成证书申请与续期,无需人工干预。详情及使用文档见:[https://docs.certimate.me](https://docs.certimate.me)。
142 1
|
23天前
|
监控 安全 Linux
在Linux中,如何管理SSL/TLS证书?
在Linux中,如何管理SSL/TLS证书?