簡介
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 數位簽章服務。
我們將探討的最強大功能之一是自訂雜湊簽署(custom hash signing),它為您的簽署工作流程提供極高彈性。此方式允許您將硬體安全模組(HSM)、智慧卡或其他任何加密裝置連接至應用程式。透過自訂雜湊簽署,開發人員可以撰寫自有的簽署服務,與專有系統、舊有基礎設施或特殊安全硬體介面,同時保持與 GroupDocs.Signature 框架的相容性。這對於有特定安全需求或已投資於加密基礎設施的組織而言,是理想的解決方案。
🔐 建立 Azure 金鑰保管庫與憑證
讓我們從設定 Azure Key Vault 並產生憑證開始。
步驟 1:佈建金鑰保管庫
使用 Azure portal 建立新的 Azure Key Vault 資源。詳細說明請參考此 quick-start guide。
💡如果您沒有有效的 Azure 訂閱,可註冊 1 個月的免費試用以開始使用。
步驟 2:產生或匯入您的憑證
金鑰保管庫啟動後,前往其儀表板,選取 Certificates 分頁,然後點擊 Generate/Import 以新增憑證。
步驟 3: 在 Method of Certificate Creation 欄位選擇 Generate,自動建立新憑證。本範例使用 Self-signed certificate 作為憑證授權類型。
備註: 若您想使用現有憑證,可選擇 Import 從本機裝置載入。
步驟 4: 前往 Advanced Policy Configuration,將 Private Key Export 選項設為 No。
步驟 5:最後,點擊 Create。憑證將被加入金鑰保管庫。
若要檢視詳細資訊,只需點擊憑證即可開啟其屬性。
⚙️ 註冊應用程式以取得 Azure Key Vault 存取權
為了讓 Web API 能存取 Azure Key Vault,必須在 Azure Active Directory 中註冊:
步驟 1: 前往 Azure Active Directory,選取 App registrations,然後點擊 New registration。
步驟 2: 輸入應用程式名稱並完成註冊。
註冊完成後,您會取得關鍵資訊——請複製 Client ID 與 Tenant ID,稍後於程式中使用。
步驟 3: 接著,從側邊選單選取 API Permissions,點擊 Add Permission。選擇 Azure Key Vault,勾選 Full Access,最後點擊 Add Permissions 完成。
步驟 4: 前往 Certificate & secrets,點擊 New client secret 以建立新密鑰。將此密鑰複製下來,以便在 .NET 應用程式中使用。
🔐 為 Azure Key Vault 指派存取原則
在前面的步驟中,我們已建立並註冊應用程式。現在需要授予該應用程式對 Azure Key Vault 的存取權:
步驟 1: 前往 Azure Key Vault,選取 Access Policies,然後點擊 Create。
步驟 2: 選擇所需的權限,然後點擊 Next。
步驟 3: 在此視窗中,選擇先前註冊的應用程式 GroupDocs.Signature.Service,然後點擊 Create。
該應用程式現在會出現在 Access Policies 區段。
📑 如何在 .NET 中使用 Azure Key Vault 與 GroupDocs.Signature 進行 PDF 簽署
本完整指南說明如何使用 GroupDocs.Signature API 於 .NET 中簽署 PDF 文件,並透過 Azure Key Vault 實作自訂雜湊簽署。我們將把流程拆解為明確步驟——從設定 C# 環境到實作符合數位簽章合規需求的自訂雜湊簽署器。
安全 PDF 簽署與自訂雜湊實作概觀
自訂雜湊簽署提供了在 .NET 應用程式中實作電子文件簽章的極高彈性。以下是本章節將涵蓋的內容:
- 設定 .NET 應用程式以使用 GroupDocs.Signature 進行 PDF 數位簽章
- 配置專業的數位簽署選項與憑證管理
- 實作自訂雜湊簽署機制以獲得最大彈性
- 整合 Azure Key Vault,安全取得公鑰憑證並完成簽署
- 在整個實作過程中遵循安全最佳實踐
欲取得更多背景資訊,請參考 GroupDocs.Signature documentation on digital signing with custom hash。
🚀 自訂雜湊簽署對 .NET PDF 應用程式的好處
在實作之前,先了解為何自訂雜湊簽署是數位簽章技術的重要突破:
- 與外部簽署裝置整合:自訂雜湊簽署可無縫連接硬體安全模組(HSM)、智慧卡、生物辨識裝置等組織已使用的專業加密硬體。
- 彈性企業架構:組織可自行開發簽署服務,與既有基礎設施、舊有系統或專有簽署解決方案介面,同時保持與 GroupDocs.Signature 的相容性。
- 提升安全合規:將雜湊產生與實際簽署分離,可更好地控制加密操作,協助符合 eIDAS、ESIGN Act 以及各行業的嚴格法規。
- 支援雲端金鑰管理:結合 Azure Key Vault,讓憑證安全儲存在 Microsoft 認證的雲端基礎設施,而非本機。
- 簽署流程客製化:實作自訂審批工作流程、多方簽署序列或特殊驗證規則,超越標準數位簽章的限制。
步驟 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...
}
}
📌Annotation:
此步驟建立新的 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
};
📌Annotation:
設定基本的數位簽章屬性(外觀、位置、雜湊演算法),確保電子文件簽章符合專業標準。
安全說明:在正式環境中,請勿將密碼等敏感資訊硬編碼;應使用符合數位簽章合規要求的安全設定方式。
步驟 3. 實作自訂雜湊簽署
現在,將自訂雜湊簽署器指派給選項。此自訂簽署器實作 ICustomSignHash 介面,將使用 Azure Key Vault 或其他外部簽署裝置完成雜湊簽署。
var azureSigner = new AzureSigner();
options.CustomSignHash = azureSigner;
options.Signature.Certificate = azureSigner.GetPublicCertificateFromAzureStorage();
📌Annotation:
此處建立 AzureSigner 實例。其 CustomSignHash 方法會在簽署過程中被呼叫,同時也會從 Azure Key Vault 取得公鑰憑證。
步驟 4. 簽署文件
最後,呼叫 Sign 方法,使用自訂雜湊簽署實作產生已簽署的 PDF。
signature.Sign(sampleOutputFilePath, options);
📌Annotation:
此方法根據先前設定的選項(包括自訂雜湊簽署器)將數位簽章套用至文件。產生的 PDF 內含可由標準 PDF 閱讀器或 GroupDocs.Signature 驗證功能驗證的加密簽章。
步驟 5. Azure 憑證簽署的完整實作
以下為使用 Azure Key Vault 進行憑證簽署的自訂簽署器完整程式碼。此類別示範了取得 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);
}
}
📌Annotations:
- Azure Credentials:
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 整合至自訂簽署服務的流程如下:
- 設定 Azure Key Vault:
- 建立 Azure Key Vault 實例。
- 上傳簽署憑證至金鑰保管庫。
- 為您的應用程式配置存取原則。
- 設定 Azure 認證:
- 在 Azure Active Directory 中註冊應用程式。
- 取得租戶 ID、用戶端 ID 與用戶端密鑰。
- 建立
ClientSecretCredential以存取金鑰保管庫。
- 取得憑證:
- 使用
CertificateClient從 Azure Key Vault 取得含公鑰的憑證。 - 此憑證用於驗證數位簽章。
- 使用
- 實作自訂簽署:
- 使用 Azure SDK 的
CryptographyClient簽署文件雜湊。 - 指定適當的簽署演算法(例如 RS256)。
- 使用 Azure SDK 的
📌Annotation:
上述每一步皆關鍵,確保簽署流程安全,且敏感金鑰僅保留於雲端,讓您能在保持彈性的同時,滿足特定安全需求或既有加密基礎設施的投資。
🧩 擴充自訂簽署實作
GroupDocs.Signature 的自訂雜湊簽署方式允許您與幾乎任何外部簽署裝置或服務整合。以下列舉幾種可能的擴充方向:
- 硬體安全模組 (HSM): 實作與實體 HSM 通訊的自訂簽署器,以獲得最高安全等級。
- 智慧卡整合: 建立與智慧卡讀卡機介面的簽署服務,適用於需要實體驗證的企業環境。
- 生物辨識驗證: 在授權簽署前,加入指紋或臉部辨識等生物驗證步驟。
- 多方批准流程: 設計多方簽署工作流程,必須取得多位使用者批准後才完成最終簽章。
- 雲端金鑰管理服務: 將範例改寫以支援 AWS KMS、Google Cloud KMS 或其他雲端金鑰管理服務。
透過實作 ICustomSignHash 介面,您可完全掌控加密操作,同時利用 GroupDocs.Signature for .NET 的強大文件處理功能。
✨ 電子文件簽章的最終思考
本指南示範了如何將 GroupDocs.Signature 與 Azure Key Vault 結合,為 PDF 檔案建立符合合規要求的安全電子文件簽章。透過此方法產生的簽章符合業界對真實性與完整性的標準。遵循本步驟說明並落實安全最佳實踐(如安全的憑證與密鑰管理),即可在 .NET 應用程式中建置穩健的數位簽署解決方案,支援整個文件工作流程。
欲取得更多細節與進階情境,請參考 official documentation。
Get a Free Trial
You can try GroupDocs.Signature APIs for free by just downloading and installing the latest version on our release downloads website.
You can also get a temporary license to test all the library’s functionalities without any constraints. Head to the temporary license page to apply for a temporary license.
🔗 See Also
For more information and additional resources, you may find the following links useful: