介绍

PDF 文件在各行各业中都至关重要,通过数字签名确保其真实性和完整性是一项关键的安全措施。数字签名提供法律效力并保护文档免受篡改。本文档将通过使用 Azure Key Vault 和 GroupDocs.Signature for .NET 创建安全的 PDF 数字签名服务,完整演示 .NET PDF 签名的实现过程。

电子文档签名在企业向无纸化工作流转型的过程中变得日益重要。GroupDocs.Signature for .NET 提供了强大的 C# PDF 数字签名解决方案,满足各种监管框架下的数字签名合规要求。无论您是 .NET 应用程序中 PDF 签名的新手,还是希望提升现有实现的开发者,本教程都涵盖了您需要了解的全部内容。

Azure Key Vault 是一项基于云的密钥管理服务,可安全存储和管理加密密钥与证书。GroupDocs.Signature for .NET 是功能强大且安全的 PDF 签名 API,允许开发者以编程方式对 PDF 文件应用数字签名。通过集成这两项技术,您可以构建一个稳健且高效的 PDF 数字签名服务。

我们将重点探讨的最强大功能之一是自定义哈希签名,它为您的签名工作流提供了极大的灵活性。该方法允许您将硬件安全模块(HSM)、智能卡或任何其他加密设备连接到应用程序。借助自定义哈希签名,开发者可以编写自己的签名服务,以对接专有系统、遗留基础设施或专用安全硬件,同时保持与 GroupDocs.Signature 框架的兼容性。这使其成为拥有特定安全需求或已有加密基础设施投入的组织的理想解决方案。

🔐 创建 Azure 密钥库和证书

让我们从设置 Azure Key Vault 并生成证书开始。

步骤 1:创建密钥库

使用 Azure portal 创建一个新的 Azure Key Vault 资源。详细操作请参阅此 快速入门指南

💡如果您没有有效的 Azure 订阅,可以注册 1 个月的免费试用以开始使用。

步骤 2:生成或导入证书

密钥库创建完成后,进入其仪表板,选择 Certificates 选项卡,点击 Generate/Import 添加证书。

Azure Key Vault interface for creating digital signature certificates for PDF signing

步骤 3:Method of Certificate Creation 字段中选择 Generate,自动创建新证书。本示例使用 Self-signed certificate 作为证书颁发机构类型。

Complete workflow diagram of GroupDocs .NET PDF digital signature process with Azure Key Vault

注意: 如果您想使用已有证书,可选择 Import 从本地设备加载。

C# code example of custom hash signing implementation with GroupDocs.Signature

步骤 4: 前往 Advanced Policy Configuration,将 Private Key Export 选项设置为 No

Azure certificate signing process diagram showing the data flow between components

步骤 5: 最后点击 Create。证书将被添加到密钥库中。

要查看证书详情,只需点击证书打开其属性。

PDF signature validation example in .NET application showing verification results

⚙️ 注册应用以启用对 Azure Key Vault 的访问

为了让 Web API 能访问 Azure Key Vault,需要在 Azure Active Directory 中注册应用:

步骤 1: 进入 Azure Active Directory,选择 App registrations,点击 New registration

Electronic document signatures integration between Azure Key Vault and GroupDocs

步骤 2: 输入应用名称并完成注册。

Secure PDF signing API configuration screen with security settings

注册完成后,您将获得关键信息——复制 Client IDTenant ID 以供应用使用。

Azure Key Vault access policy setup interface for digital signature permissions

步骤 3: 在侧边菜单中选择 API Permissions,点击 Add Permission。选择 Azure Key Vault,勾选 Full Access,最后点击 Add Permissions 完成。

.NET PDF signing output example showing a successfully signed document

步骤 4: 选择 Certificate & secrets,点击 New client secret 创建新密钥。复制该密钥,以便在 .NET 应用中使用。

External signing device integration diagram for GroupDocs custom hash implementation

🔐 为 Azure Key Vault 分配访问策略

在前面的步骤中,我们已经创建并注册了应用。现在需要授予该应用对 Azure Key Vault 的访问权限:

步骤 1: 进入 Azure Key Vault,选择 Access Policies,点击 Create

Digital signature compliance settings configuration for regulatory requirements

步骤 2: 选择所需的权限后点击 Next

Azure Active Directory application registration for PDF signing services

步骤 3: 在弹窗中选择之前注册的应用 GroupDocs.Signature.Service,然后点击 Create

Custom sign services architecture diagram showing component relationships
GroupDocs Signature appearance settings panel for visual signature customization

该应用现在会出现在 Access Policies 区域。

PDF digital signature verification process showing validation workflow steps

📑 如何在 .NET 中使用 Azure Key Vault 与 GroupDocs.Signature 进行 PDF 签名

本指南详细说明了如何使用 GroupDocs.Signature API 在 .NET 中通过自定义哈希签名并借助 Azure Key Vault 对 PDF 文档进行签名。我们将把整个过程拆解为清晰的步骤——从配置 C# 环境到实现满足数字签名合规要求的自定义哈希签名器。

安全 PDF 签名与自定义哈希实现概览

自定义哈希签名方法为在 .NET 应用中实现电子文档签名提供了极大的灵活性。我们将覆盖以下内容:

  • 在 .NET 应用中配置 GroupDocs.Signature 以进行 PDF 数字签名
  • 使用正确的证书管理配置专业的数字签名选项
  • 实现自定义哈希签名机制以获得最大灵活性
  • 集成 Azure Key Vault 安全获取公钥证书并完成签名
  • 在整个实现过程中遵循安全最佳实践

更多背景信息,请参阅 GroupDocs.Signature 文档中关于自定义哈希数字签名的章节

🚀 自定义哈希签名对 .NET PDF 应用的优势

在实现之前,先了解为何自定义哈希签名是数字签名技术的重要进步:

  1. 与外部签名设备集成:自定义哈希签名可无缝对接硬件安全模块(HSM)、智能卡、生物识别设备等组织已有的专用加密硬件。
  2. 企业级灵活架构:组织可以编写自己的签名服务,连接现有基础设施、遗留系统或专有签名方案,同时保持与 GroupDocs.Signature 的兼容性。
  3. 提升安全合规性:将哈希生成与实际签名过程分离,可更好地控制加密操作,帮助满足 eIDAS、ESIGN Act 以及行业特定合规标准。
  4. 支持云密钥管理:与 Azure Key Vault 集成后,证书安全存储在微软经过认证的云基础设施中,而非本地机器。
  5. 签名流程自定义:实现自定义审批工作流、多方签名顺序或特殊验证规则,超越标准数字签名实现的限制。

第 1 步. 配置 C# PDF 数字签名环境

首先在 C# 应用中定义文件路径并初始化 GroupDocs.Signature 对象。下面示例指定源 PDF 文件以及签名后文档的输出位置。

public static void SignDocument()  
{  
    // Define paths for the source and output documents  
    string sampleFilePath = "sample.pdf";  
    string sampleOutputFilePath = "signed.pdf";

    using (Signature signature = new Signature(sampleFilePath))  
    {  
        // Code continues in the next steps...  
    }  
}

📌注释:

此步骤创建了一个新的 Signature 实例,用于加载待签名的 PDF 文档,为 .NET 应用中的安全 PDF 签名奠定基础。


第 2 步. 配置数字签名选项

接下来设置数字签名选项,包括证书信息、可视外观以及哈希算法。自定义哈希签名将在后续插入。

// Initialize digital signing options  
DigitalSignOptions options = new DigitalSignOptions()  
{  
    Signature = new DigitalSignature(),  
    Password = "1234567890",      // Certificate password  
    Reason = "Sign",              // Signing reason  
    Contact = "JohnSmith",        // Contact information  
    Location = "Office1",         // Signing location  
    AllPages = true,              // Apply signature on all pages  
    Width = 80,  
    Height = 60,  
    VerticalAlignment = VerticalAlignment.Bottom,  
    HorizontalAlignment = HorizontalAlignment.Right,  
    Margin = new Padding() { Bottom = 10, Right = 10 },  
    HashAlgorithm = HashAlgorithm.Sha256 // Specify SHA-256 for hashing  
};

📌注释:

配置基本的数字签名属性(如外观、位置和哈希),确保电子文档签名符合专业标准。

安全提示:生产环境中请勿在代码中硬编码敏感信息(如密码),应使用符合数字签名合规要求的安全配置方式。


第 3 步. 实现自定义哈希签名

现在,将自定义哈希签名器分配给选项。该签名器实现了 ICustomSignHash 接口,可用于通过 Azure Key Vault 或其他外部签名设备完成哈希签名。

var azureSigner = new AzureSigner();  
options.CustomSignHash = azureSigner;  
options.Signature.Certificate = azureSigner.GetPublicCertificateFromAzureStorage();

📌注释:

此处创建 AzureSigner 实例。其 CustomSignHash 方法将在签名过程中被调用,同时它还会从 Azure Key Vault 获取公钥证书。


第 4 步. 对文档进行签名

最后,调用 Sign 方法使用自定义哈希签名实现生成签名后的 PDF 文档。

signature.Sign(sampleOutputFilePath, options);

📌注释:

该方法依据前面配置的选项(包括自定义哈希签名器)将数字签名应用到文档。生成的 PDF 包含可通过标准 PDF 阅读器或使用 GroupDocs.Signature 验证功能进行验证的加密签名。


第 5 步. Azure 证书签名的详细实现

以下代码展示了使用 Azure Key Vault 进行证书签名的完整自定义签名器实现。该类演示了专业的 Azure 证书签名实践,包括获取 Azure 凭证、获取公钥证书以及安全地对文档哈希进行签名。

public class AzureSigner : ICustomSignHash  
{  
    public byte[] CustomSignHash(byte[] hash, HashAlgorithm hashAlgorithm,
     SignatureContext signatureContext)  
    {  
        // Delegate the signing operation to Azure  
        return SignWithAzure(hash);  
    }

    private static byte[] SignWithAzure(byte[] signableHash)  
    {  
        // Retrieve Azure credentials  
        var credential = GetAzureSecretCredential();

        // Specify your Key Vault certificate key identifier  
        var certificateKeyId = 
        "https://groupdocskeyvault.vault.azure.net/keys/GroupDocsSignatureCertificate/>";  
        CryptographyClient client = 
            new CryptographyClient(new Uri(certificateKeyId), credential);

        // Sign the hash using RS256  
        var result = client.Sign(SignatureAlgorithm.RS256, signableHash);  
        return result.Signature;  
    }

    static ClientSecretCredential GetAzureSecretCredential()  
    {  
        // Provide your Azure AD tenant, client, and secret details  
        string tenantId = "your tenant id";  
        string clientId = "your client id";  
        string secret = "your secret";  
        ClientSecretCredential credential = 
            new ClientSecretCredential(tenantId, clientId, secret);  
        return credential;  
    }

    public X509Certificate2 GetPublicCertificateFromAzureStorage()  
    {  
        // Define the Key Vault URI  
        string vaultUri = "https://groupdocskeyvault.vault.azure.net/>";  
        var credential  = GetAzureSecretCredential();  
        X509Certificate2 pubCertificate = 
        GetPublicCertificateFromAzureStorage(credential, vaultUri);  
        return pubCertificate;  
    }

    static X509Certificate2 GetPublicCertificateFromAzureStorage(
        ClientSecretCredential credential, string uri)  
    {  
        // Create a certificate client for the Key Vault  
        CertificateClient certificateClient = 
            new CertificateClient(new Uri(uri), credential);

        // Retrieve the certificate with the public key  
        KeyVaultCertificateWithPolicy certificate = 
        certificateClient.GetCertificateAsync("GroupDocsSignatureCertificate").Result;  
        return new X509Certificate2(certificate.Cer);  
    }  
}

📌注释:

  • Azure 凭证GetAzureSecretCredential 方法使用租户 ID、客户端 ID 和密钥创建凭证。安全提示:请使用 Azure Key Vault 或安全配置存储来保存这些凭证,以确保 .NET PDF 签名实现的完整性。
  • 使用 Azure 签名SignWithAzure 方法通过 Azure SDK 的 CryptographyClient 使用存储在 Azure Key Vault 中的证书对哈希进行签名,展示了符合数字签名合规最佳实践的专业证书管理方式。
  • 获取公钥证书GetPublicCertificateFromAzureStorage 连接 Azure Key Vault 并检索用于签名验证的证书,确保电子文档签名能够被正确验证。

第 6 步. Azure Key Vault 在 PDF 数字签名中的实现步骤

将 Azure Key Vault 与自定义签名服务集成,请按以下步骤操作:

  1. 设置 Azure Key Vault
    • 创建 Azure Key Vault 实例。
    • 将签名证书上传至 Key Vault。
    • 为应用配置访问策略。
  2. 配置 Azure 凭证
    • 在 Azure Active Directory 中注册应用。
    • 获取租户 ID、客户端 ID 与客户端密钥。
    • 创建 ClientSecretCredential 用于访问 Key Vault。
  3. 检索证书
    • 使用 CertificateClient 从 Azure Key Vault 获取包含公钥的证书。
    • 该证书用于验证数字签名。
  4. 实现自定义签名
    • 使用 Azure SDK 的 CryptographyClient 对文档哈希进行签名。
    • 指定合适的签名算法(如 RS256)。

📌注释:

上述每一步都是确保签名过程安全、密钥始终受云端保护的关键环节,同时为实现自定义签名服务提供了灵活的扩展空间。


🧩 扩展自定义签名实现

GroupDocs.Signature 的自定义哈希签名方式的优势在于几乎可以与任何外部签名设备或服务配合使用。以下是一些可能的扩展思路:

  1. 硬件安全模块(HSM):实现与物理 HSM 通信的自定义签名器,以获得最高安全级别。
  2. 智能卡集成:为需要实体认证的企业环境开发智能卡读取器签名器。
  3. 生物识别认证:在签名前加入指纹或面部识别等生物验证步骤。
  4. 多方审批:实现多方签署工作流,只有全部批准后才完成最终签名。
  5. 云服务提供商:将示例改造为兼容 AWS KMS、Google Cloud KMS 或其他云密钥管理服务。

通过实现 ICustomSignHash 接口,您可以在保持对加密操作完整控制的同时,利用 GroupDocs.Signature for .NET 强大的文档处理能力。


✨ 电子文档签名的最终思考

本指南展示了如何将 GroupDocs.Signature 与 Azure Key Vault 结合,安全、合规地为 PDF 文件创建电子文档签名。通过此方法生成的签名符合行业对真实性和完整性的标准。遵循本文的分步指南并采纳安全最佳实践(如安全凭证管理),即可在 .NET 应用中实现稳健的数字签名解决方案,覆盖整个文档工作流。

欲了解更多细节和高级场景,请查阅 官方文档


获取免费试用

您可以通过下载并安装我们 release downloads website 上的最新版本,免费试用 GroupDocs.Signature API。

同时,您还可以获取临时许可证,以在无任何限制的情况下测试库的全部功能。前往 temporary license page 申请临时许可证。

🔗 另见

更多信息和资源,请参考以下链接: