你当前正在访问 Microsoft Azure Global Edition 技术文档网站。 如果需要访问由世纪互联运营的 Microsoft Azure 中国技术文档网站,请访问 https://docs.azure.cn

在应用程序代码中使用 TLS/SSL 证书

在应用程序代码中,可以访问 添加到 Azure 应用服务的公共或专用证书。 应用代码可能充当客户端并访问需要证书身份验证的外部服务。 它可能需要执行加密任务。 本文介绍如何在应用程序代码中使用公共或专用证书。

在代码中使用证书的方式利用了应用服务的传输层安全性(TLS)功能,这要求您的应用处于基本层级或更高层级。 如果应用位于“免费”或“共享”层中,则可以 在应用存储库中包含证书文件

允许应用服务管理 TLS/安全套接字层(SSL)证书时,可以单独维护证书和应用程序代码,并保护敏感数据。

先决条件

若要遵循本文,请参阅:

查找指纹

  1. Azure 门户中的左窗格中,选择 “应用服务><”应用名称>

  2. 在应用的左窗格中,选择“ 证书”。 然后选择“自带证书”(.pfx)公钥证书(.cer)。

  3. 找到要使用的证书并复制指纹。

显示复制证书指纹的屏幕截图。

使证书易于访问

若要访问应用代码中的证书,请将其指纹添加到 WEBSITE_LOAD_CERTIFICATES 应用设置。 在 Azure Cloud Shell 中运行以下命令:

az webapp config appsettings set --name <app-name> --resource-group <resource-group-name> --settings WEBSITE_LOAD_CERTIFICATES=<comma-separated-certificate-thumbprints>

若要使所有证书都可访问,请将值设置为 *

WEBSITE_LOAD_CERTIFICATES设置为*时,所有之前添加的证书均可供应用程序代码访问。 如果稍后将证书添加到应用,请重启应用以使新证书可供应用访问。 有关详细信息,请参阅 更新或续订证书

在 Windows 应用中加载证书

应用 WEBSITE_LOAD_CERTIFICATES 设置使指定的证书可供 Windows 证书存储中的 Windows 托管应用( 当前用户\我的)访问。

在 C# 代码中,使用证书指纹访问证书。 以下代码使用指纹 E661583E8FABEF4C0BEF694CBC41C28FB81CD870加载证书。

using System;
using System.Linq;
using System.Security.Cryptography.X509Certificates;

string certThumbprint = "E661583E8FABEF4C0BEF694CBC41C28FB81CD870";
bool validOnly = false;

using (X509Store certStore = new X509Store(StoreName.My, StoreLocation.CurrentUser))
{
  certStore.Open(OpenFlags.ReadOnly);

  X509Certificate2Collection certCollection = certStore.Certificates.Find(
                              X509FindType.FindByThumbprint,
                              // Replace below with your certificate's thumbprint
                              certThumbprint,
                              validOnly);
  // Get the first cert with the thumbprint
  X509Certificate2 cert = certCollection.OfType<X509Certificate2>().FirstOrDefault();

  if (cert is null)
      throw new Exception($"Certificate with thumbprint {certThumbprint} was not found");

  // Use certificate
  Console.WriteLine(cert.FriendlyName);
  
  // Consider to call Dispose() on the certificate after it's being used, available in .NET 4.6 and later
}

在 Java 代码中,使用“使用者公用名”字段从存储区Windows-MY访问证书。 有关详细信息,请参阅 公钥证书。 以下代码演示如何加载私钥证书:

import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RequestMapping;
import java.security.KeyStore;
import java.security.cert.Certificate;
import java.security.PrivateKey;

...
KeyStore ks = KeyStore.getInstance("Windows-MY");
ks.load(null, null); 
Certificate cert = ks.getCertificate("<subject-cn>");
PrivateKey privKey = (PrivateKey) ks.getKey("<subject-cn>", ("<password>").toCharArray());

// Use the certificate and key
...

有关不支持或提供对 Windows 证书存储的支持不足的语言,请参阅 从文件加载证书

从文件加载证书

如果需要手动加载上传的证书文件,最好使用 文件传输协议安全(FTPS) 而不是 Git 上传证书。 请将敏感数据(如私人证书)排除在源代码管理之外。

即使从文件加载证书,Windows 上的 ASP.NET 和 ASP.NET Core 也必须访问证书存储。 若要在 Windows .NET 应用中加载证书文件,请使用 Cloud Shell 中的以下命令加载当前用户配置文件:

az webapp config appsettings set --name <app-name> --resource-group <resource-group-name> --settings WEBSITE_LOAD_USER_PROFILE=1

这种在代码中使用证书的方法利用了应用服务中的 TLS 功能,这需要你的应用处于基础级别或更高的层级。

以下 C# 示例从应用中的相对路径加载公共证书:

using System;
using System.IO;
using System.Security.Cryptography.X509Certificates;

...
var bytes = File.ReadAllBytes("~/<relative-path-to-cert-file>");
var cert = new X509Certificate2(bytes);

// Use the loaded certificate

若要了解如何从 Node.js、PHP、Python 或 Java 中的文件加载 TLS/SSL 证书,请参阅相应语言或 Web 平台的文档。

在 Linux/Windows 容器中加载证书

应用 WEBSITE_LOAD_CERTIFICATES 设置使指定的证书可供 Windows 或 Linux 自定义容器(包括内置 Linux 容器)作为文件访问。 这些文件位于以下目录下:

容器平台 公开证书 专用证书
Windows 容器 C:\appservice\certificates\public C:\appservice\certificates\private
Linux 容器 /var/ssl/certs /var/ssl/private

证书文件名是证书指纹。

注释

应用服务将证书路径作为以下环境变量注入 Windows 容器: WEBSITE_PRIVATE_CERTS_PATHWEBSITE_INTERMEDIATE_CERTS_PATHWEBSITE_PUBLIC_CERTS_PATHWEBSITE_ROOT_CERTS_PATH。 最好使用环境变量引用证书路径,而不是硬编码证书路径,以防证书路径在将来发生更改。

此外, Windows Server Core 和 Windows Nano Server 容器 会自动将证书加载到证书存储中 LocalMachine\My。 若要加载证书,请遵循与 Windows 应用中的“加载证书”中显示的模式相同。 对于基于 Windows Nano 的容器,请使用文件路径,如 从文件加载证书中所示。

以下 C# 代码演示如何在 Linux 应用中加载公共证书。

using System;
using System.IO;
using System.Security.Cryptography.X509Certificates;

...
var bytes = File.ReadAllBytes("/var/ssl/certs/<thumbprint>.der");
var cert = new X509Certificate2(bytes);

// Use the loaded certificate

以下 C# 代码演示如何在 Linux 应用中加载专用证书。

using System;
using System.IO;
using System.Security.Cryptography.X509Certificates;
...
var bytes = File.ReadAllBytes("/var/ssl/private/<thumbprint>.p12");
var cert = new X509Certificate2(bytes);

// Use the loaded certificate

若要了解如何从 Node.js、PHP、Python 或 Java 中的文件加载 TLS/SSL 证书,请参阅相应语言或 Web 平台的文档。

更新或续订证书

续订证书并将其添加到应用时,会获取新的指纹,还必须使之可访问。 工作原理取决于证书类型。

如果手动上传 公共专用 证书:

  • 如果在 WEBSITE_LOAD_CERTIFICATES 中显式列出指纹,请接着将新指纹添加到应用设置中。
  • 如果 WEBSITE_LOAD_CERTIFICATES 设置为 *,请重启应用以使新证书可访问。

如果在 Azure Key Vault 中续订证书(例如使用 应用服务证书),则当应用与续订的证书同步时,Key Vault 中的每日同步会自动进行必要的更新。

  • 如果 WEBSITE_LOAD_CERTIFICATES 包含续订的证书的旧指纹,则每日同步会自动将旧指纹更新为新的指纹。
  • 如果 WEBSITE_LOAD_CERTIFICATES 设置为 *,则每日同步可自动访问新证书。