翻譯|使用教程|編輯:吉煒煒|2025-01-08 10:59:40.623|閱讀 116 次
概述:本文介紹如何使用 .NET C# 中的 Azure Key Vault 中的 PFX 證書(shū)對(duì) PDF 文檔進(jìn)行簽名。示例代碼展示了如何從 Azure Key Vault 加載證書(shū)以及如何使用該證書(shū)對(duì) PDF 文檔進(jìn)行簽名。
# 界面/圖表報(bào)表/文檔/IDE等千款熱門(mén)軟控件火熱銷(xiāo)售中 >>
相關(guān)鏈接:
TX Text Control 是一款功能類(lèi)似于 MS Word 的文字處理控件,包括文檔創(chuàng)建、編輯、打印、郵件合并、格式轉(zhuǎn)換、拆分合并、導(dǎo)入導(dǎo)出、批量生成等功能。廣泛應(yīng)用于企業(yè)文檔管理,網(wǎng)站內(nèi)容發(fā)布,電子病歷中病案模板創(chuàng)建、病歷書(shū)寫(xiě)、修改歷史、連續(xù)打印、病案歸檔等功能的實(shí)現(xiàn)。
Azure Key Vault 是 Microsoft Azure 提供的一項(xiàng)云服務(wù),用于安全地存儲(chǔ)和訪問(wèn)敏感信息(例如 API 密鑰、密碼、證書(shū)和加密密鑰)。它充當(dāng)管理機(jī)密和加密操作的中央保管庫(kù),為靜態(tài)和傳輸中的數(shù)據(jù)提供強(qiáng)大的保護(hù)。
Azure Key Vault 為使用 PFX 證書(shū)對(duì) PDF 文檔進(jìn)行簽名提供了幾個(gè)關(guān)鍵優(yōu)勢(shì)。最重要的是,它通過(guò)將敏感證書(shū)存儲(chǔ)在集中、高度安全的環(huán)境中來(lái)提高安全性,從而消除了在本地設(shè)備或服務(wù)器上暴露的風(fēng)險(xiǎn)。私鑰永遠(yuǎn)不會(huì)離開(kāi)保管庫(kù),因?yàn)榧用懿僮魇窃诒9軒?kù)內(nèi)執(zhí)行的。
這種方法簡(jiǎn)化了證書(shū)管理,并允許組織集中和簡(jiǎn)化密鑰輪換、撤銷(xiāo)和審核。 Azure Key Vault 還提供可擴(kuò)展性,以無(wú)縫滿足不斷增長(zhǎng)的應(yīng)用程序的需求,同時(shí)保持高可用性和強(qiáng)大的性能。合規(guī)性是另一個(gè)主要優(yōu)勢(shì),因?yàn)樵摲?wù)有助于滿足嚴(yán)格的行業(yè)標(biāo)準(zhǔn)和法規(guī),例如 GDPR、HIPAA 和 PCI DSS,從而確保數(shù)據(jù)保護(hù)和隱私。
這些優(yōu)勢(shì)結(jié)合起來(lái),提供了一種安全、可靠且經(jīng)濟(jì)高效的方法來(lái)管理 PDF 簽名和其他加密操作的證書(shū)。
本文介紹如何使用存儲(chǔ)在 Azure Key Vault 中的 PFX 證書(shū)對(duì) PDF 文檔進(jìn)行簽名。以下屏幕截圖顯示了已上傳 PFX 證書(shū)的 Azure Key Vault 門(mén)戶:
Azure 需要以下 NuGet 包:
以下代碼展示了將本地 PFX 文件上傳到 Azure Key Vault 的一種方法,盡管本文不應(yīng)重點(diǎn)介紹如何上傳證書(shū)。如果您擁有適當(dāng)?shù)挠脩粼L問(wèn)權(quán)限,也可以將證書(shū)上傳到門(mén)戶。
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 證書(shū)上傳到 Azure Key Vault 后,可以使用 Azure SDK 訪問(wèn)該證書(shū)并簽署 PDF 文檔。以下代碼片段演示了如何使用存儲(chǔ)在 Azure Key Vault 中的 PFX 證書(shū)簽署 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."); |
當(dāng)您在 Acrobat Reader 中打開(kāi)創(chuàng)建的 PDF 文檔時(shí),您可以在簽名面板中看到有效的證書(shū)。
使用 Azure Key Vault 存儲(chǔ)用于 PDF 簽名的 PFX 證書(shū),為管理敏感加密操作提供了一種安全、可擴(kuò)展且合規(guī)的解決方案。通過(guò)使用 Azure Key Vault,組織可以提高安全性、簡(jiǎn)化證書(shū)管理并確保符合行業(yè)標(biāo)準(zhǔn)和法規(guī)。
本站文章除注明轉(zhuǎn)載外,均為本站原創(chuàng)或翻譯。歡迎任何形式的轉(zhuǎn)載,但請(qǐng)務(wù)必注明出處、不得修改原文相關(guān)鏈接,如果存在內(nèi)容上的異議請(qǐng)郵件反饋至chenjj@fc6vip.cn