This C# sample downloads a certificate with its private key from Azure Key Vault and signs a PDF file with it. The certificate must be created or imported in Key Vault with an exportable private key: Azure stores it as a PKCS#12 (PFX) secret with the same name.
How it works
SecretClient.GetSecret(NuGet packages Azure.Identity and Azure.Security.KeyVault.Secrets) reads the secret of the certificate: a Base64 encoded PFX file without password.DigitalCertificate.LoadCertificate(bytes, string.Empty)loads it, and the PDF is signed as usual.- The application is authenticated with
DefaultAzureCredential(environment variables, managed identity, Visual Studio, Azure CLI); the identity needs the permission “Get” on the secrets.
C# code
/// <summary>
/// Downloads a certificate, with its private key, from Azure Key Vault. The certificate is returned as the bytes of a PFX file.
/// </summary>
static byte[] DownloadCertificateFromAzureKeyVault(string keyVaultUrl, string vaultCertificateName)
{
SecretClient secretClient = new SecretClient(new Uri(keyVaultUrl), new DefaultAzureCredential());
// A certificate stored in Azure Key Vault has a secret with the same name: the secret contains the certificate with its
// private key (only when the private key is exportable).
KeyVaultSecret secret = secretClient.GetSecret(vaultCertificateName).Value;
// The secret of a certificate with a PKCS#12 content type is the Base64 encoded PFX file (without password).
// A certificate with a PEM content type has to be converted before it is used.
if (secret.Properties.ContentType != "application/x-pkcs12")
throw new Exception("The content type of the certificate is " + secret.Properties.ContentType + ". Create or import the certificate with the content type PKCS#12 (PFX).");
return Convert.FromBase64String(secret.Value);
}
/// <summary>
/// Signs a PDF file with a certificate downloaded from Azure Key Vault.
/// </summary>
static void SignPdfWithAzureKeyVaultCertificate(string unsignedPdfFile, string signedPdfFile)
{
PdfSignature pdfSignature = new PdfSignature(LibrarySerialNumber);
pdfSignature.LoadPdfDocument(unsignedPdfFile);
pdfSignature.SigningReason = "I approve this document";
pdfSignature.SigningLocation = "Accounting department";
pdfSignature.SignaturePosition = SignaturePosition.TopLeft;
pdfSignature.HashAlgorithm = SignLib.HashAlgorithm.SHA256;
// The certificate is downloaded as a PFX file without password and it is loaded from the bytes of the file.
byte[] azureKeyVaultCertificate = DownloadCertificateFromAzureKeyVault(KeyVaultUrl, VaultCertificateName);
pdfSignature.DigitalSignatureCertificate = DigitalCertificate.LoadCertificate(azureKeyVaultCertificate, string.Empty);
File.WriteAllBytes(signedPdfFile, pdfSignature.ApplyDigitalSignature());
Console.WriteLine("The PDF signature was created." + Environment.NewLine);
}
Here the private key leaves Azure Key Vault (it is downloaded into the memory of the application). When the key must never leave Azure, use a non-exportable key: Sign PDF with a non-exportable Azure Key Vault HSM key.
Run the full sample. The complete Visual Studio project PDF Digital Signature with Azure Key Vault Certificate is included in the library download, in the folder NET Sample Projects (.NET 8, .NET 9, .NET 10 and .NET 11), with the test files and a test certificate. Without a serial number the library runs in demo mode. Download the library and the samples · User manual
Frequently asked questions
Why does the sample fail with a PEM certificate?
The sample expects the content type PKCS#12 (application/x-pkcs12). Create or import the certificate in Key Vault with the PKCS#12 content type, or convert the PEM content before loading it.