翻譯|使用教程|編輯:吉煒煒|2025-01-08 10:59:40.623|閱讀 116 次
概述:本文介紹如何使用 .NET C# 中的 Azure Key Vault 中的 PFX 證書對 PDF 文檔進行簽名。示例代碼展示了如何從 Azure Key Vault 加載證書以及如何使用該證書對 PDF 文檔進行簽名。
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
相關鏈接:
TX Text Control 是一款功能類似于 MS Word 的文字處理控件,包括文檔創建、編輯、打印、郵件合并、格式轉換、拆分合并、導入導出、批量生成等功能。廣泛應用于企業文檔管理,網站內容發布,電子病歷中病案模板創建、病歷書寫、修改歷史、連續打印、病案歸檔等功能的實現。
Azure Key Vault 是 Microsoft Azure 提供的一項云服務,用于安全地存儲和訪問敏感信息(例如 API 密鑰、密碼、證書和加密密鑰)。它充當管理機密和加密操作的中央保管庫,為靜態和傳輸中的數據提供強大的保護。
Azure Key Vault 為使用 PFX 證書對 PDF 文檔進行簽名提供了幾個關鍵優勢。最重要的是,它通過將敏感證書存儲在集中、高度安全的環境中來提高安全性,從而消除了在本地設備或服務器上暴露的風險。私鑰永遠不會離開保管庫,因為加密操作是在保管庫內執行的。
這種方法簡化了證書管理,并允許組織集中和簡化密鑰輪換、撤銷和審核。 Azure Key Vault 還提供可擴展性,以無縫滿足不斷增長的應用程序的需求,同時保持高可用性和強大的性能。合規性是另一個主要優勢,因為該服務有助于滿足嚴格的行業標準和法規,例如 GDPR、HIPAA 和 PCI DSS,從而確保數據保護和隱私。
這些優勢結合起來,提供了一種安全、可靠且經濟高效的方法來管理 PDF 簽名和其他加密操作的證書。
本文介紹如何使用存儲在 Azure Key Vault 中的 PFX 證書對 PDF 文檔進行簽名。以下屏幕截圖顯示了已上傳 PFX 證書的 Azure Key Vault 門戶:
Azure 需要以下 NuGet 包:
以下代碼展示了將本地 PFX 文件上傳到 Azure Key Vault 的一種方法,盡管本文不應重點介紹如何上傳證書。如果您擁有適當的用戶訪問權限,也可以將證書上傳到門戶。
using System; | |
using System.Security.Cryptography.X509Certificates; | |
using Azure.Identity; | |
using Azure.Security.KeyVault.Certificates; | |
string keyVaultUrl = "http://txtestvault.vault.azure.net/"; | |
string certificateName = "txselfcert"; | |
string password = "123"; | |
// Create a CertificateClient using DefaultAzureCredential for authentication | |
var credential = new DefaultAzureCredential(true); | |
var client = new CertificateClient(new Uri(keyVaultUrl), credential); | |
// Read the PFX file into a byte array | |
byte[] pfxBytes = await File.ReadAllBytesAsync("certificate.pfx"); | |
// Import the certificate into Key Vault | |
var importOptions = new ImportCertificateOptions(certificateName, pfxBytes) | |
{ | |
Password = password | |
}; | |
var importedCertificate = await client.ImportCertificateAsync(importOptions); | |
Console.WriteLine($"Certificate '{certificateName}' imported successfully."); |
將 PFX 證書上傳到 Azure Key Vault 后,可以使用 Azure SDK 訪問該證書并簽署 PDF 文檔。以下代碼片段演示了如何使用存儲在 Azure Key Vault 中的 PFX 證書簽署 PDF 文檔:
using System; | |
using System.Security.Cryptography.X509Certificates; | |
using Azure.Identity; | |
using Azure.Security.KeyVault.Certificates; | |
using TXTextControl; | |
string keyVaultUrl = "http://txtestvault.vault.azure.net/"; | |
string certificateName = "txselfcert"; | |
// Create a CertificateClient using DefaultAzureCredential for authentication | |
var credential = new DefaultAzureCredential(true); | |
var client = new CertificateClient(new Uri(keyVaultUrl), credential); | |
// Retrieve the certificate from the Azure Key Vault | |
var certificateWithPolicy = client.GetCertificate(certificateName); | |
byte[] pfxBytes = certificateWithPolicy.Value.Cer; // Extract the certificate's byte array | |
// Load the certificate into an X509Certificate2 object for digital signature purposes | |
var cert = new X509Certificate2(pfxBytes, (string)null, X509KeyStorageFlags.PersistKeySet); | |
// Initialize TXTextControl to create and save a document with the digital signature | |
using (var tx = new ServerTextControl()) | |
{ | |
tx.Create(); | |
tx.Text = "Hello, World!"; | |
// Prepare the digital signature for the document | |
var saveSettings = new SaveSettings | |
{ | |
DigitalSignature = new DigitalSignature(cert, null) | |
}; | |
// Save the document as a PDF with the digital signature | |
tx.Save("result.pdf", StreamType.AdobePDF, saveSettings); | |
} | |
Console.WriteLine("PDF saved with digital signature."); |
當您在 Acrobat Reader 中打開創建的 PDF 文檔時,您可以在簽名面板中看到有效的證書。
使用 Azure Key Vault 存儲用于 PDF 簽名的 PFX 證書,為管理敏感加密操作提供了一種安全、可擴展且合規的解決方案。通過使用 Azure Key Vault,組織可以提高安全性、簡化證書管理并確保符合行業標準和法規。
本站文章除注明轉載外,均為本站原創或翻譯。歡迎任何形式的轉載,但請務必注明出處、不得修改原文相關鏈接,如果存在內容上的異議請郵件反饋至chenjj@fc6vip.cn