'''' This code is part of Document Solutions for PDF .NET demos.'' Copyright (c) MESCIUS inc. All rights reserved.''ImportsSystemImportsSystem.IOImportsSystem.DrawingImportsSystem.Security.Cryptography.X509CertificatesImportsOrg.BouncyCastle.Crypto.DigestsImportsAzure.CoreImportsAzure.IdentityImportsAzure.Security.KeyVault.CertificatesImportsAzure.Security.KeyVault.Keys.CryptographyImportsGrapeCity.Documents.PdfImportsGrapeCity.Documents.Pdf.Security '' This sample shows how to sign an existing PDF file that contains'' an empty signature field with a certificate that is stored'' in an Azure Key Vault.'''' The sample includes a ready to use utility class AzureSignatureGenerator'' that implements the GrapeCity.Documents.Pdf.IPkcs7SignatureGenerator interface,'' and can be used to sign PDFs with certificates stored in Azure Key Vault.'''' Please note that when run directly off the DsPdf demo site,'' this sample will NOT sign the PDF, as it passes dummy Azure credentials'' to the AzureSignatureGenerator's ctor. You will need to download the sample'' and provide your own credentials for the sample code to actually sign a PDF.PublicClassSignAzureKeyVaultFunctionCreatePDF(ByVal stream AsStream) AsIntegerDim doc = NewGcPdfDocument()Using s = File.OpenRead(Path.Combine("Resources", "PDFs", "SignAzureKeyVault.pdf")) doc.Load(s) Try'' This WILL NOT WORK due to dummy Azure credentials.'' Supply valid credentials to actually sign the PDF.Using sg = NewAzureSignatureGenerator("keyVaultName","tenantId","clientId","clientSecret","certificateName") Dim sp = NewSignatureProperties() With { .SignatureBuilder = NewPkcs7SignatureBuilder() With { .SignatureGenerator = sg, .CertificateChain = NewX509Certificate2() {sg.Certificate} }, .SignatureField = doc.AcroForm.Fields(0) } doc.Sign(sp, stream)EndUsingCatch e AsExceptionDim page = doc.Pages(0)Dim r = doc.AcroForm.Fields(0).Widgets(0).RectUtil.AddNote("Signing failed because dummy Azure credentials were used." & vbLf &"Use valid Azure Key Vault credentials to sign the PDF.", page,NewRectangleF(r.Left, r.Bottom + 24, page.Size.Width - r.Left * 2, 0)) doc.Save(stream)EndTryEndUsing '' Done.Return doc.Pages.CountEndFunctionEndClass ''' <summary>''' Implements IPkcs7SignatureGenerator''' and allows generating a digital signature using''' a certificate stored in Azure Key Vault.''' </summary>PublicClassAzureSignatureGeneratorImplementsIPkcs7SignatureGeneratorImplementsIDisposable Private_certificateClientAsCertificateClientPrivate_certificateAsX509Certificate2Private_cryptographyClientAsCryptographyClient ''' <summary>''' Initializes a new instance of the AzureSignatureGenerator class.''' </summary>PublicSubNew(ByVal keyVaultName AsString,ByVal tenantId AsString,ByVal clientId AsString,ByVal clientSecret AsString,ByVal certificateName AsString)Dim keyVaultUri = NewUri($"https://{keyVaultName}.vault.azure.net/")Dim credential AsTokenCredential = NewClientSecretCredential(tenantId, clientId, clientSecret)_certificateClient = NewCertificateClient(keyVaultUri, credential)Dim c = _certificateClient.GetCertificate(certificateName)_certificate = NewX509Certificate2(c.Value.Cer)_cryptographyClient = NewCryptographyClient(c.Value.KeyId, credential)EndSub ''' <summary>''' Gets the ID of the hash algorithm.''' </summary>PublicReadOnlyPropertyHashAlgorithmAsOIDImplementsIPkcs7SignatureGenerator.HashAlgorithmGetReturnOID.HashAlgorithms.SHA256EndGetEndProperty ''' <summary>''' Gets the ID of the encryption algorithm.''' </summary>PublicReadOnlyPropertyDigestEncryptionAlgorithmAsOIDImplementsIPkcs7SignatureGenerator.DigestEncryptionAlgorithmGetReturnOID.EncryptionAlgorithms.RSAEndGetEndProperty ''' <summary>''' Gets the certificate.''' </summary>PublicReadOnlyPropertyCertificateAsX509Certificate2GetReturn_certificateEndGetEndProperty ''' <summary>''' Signs data.''' </summary>PublicFunctionSignData(ByVal input AsByte()) AsByte() ImplementsIPkcs7SignatureGenerator.SignDataDim hashDigest = NewSha256Digest()Dim hash = NewByte(hashDigest.GetDigestSize() - 1) {} hashDigest.Reset() hashDigest.BlockUpdate(input, 0, input.Length) hashDigest.DoFinal(hash, 0)Dim result AsByte() = _cryptographyClient.Sign(SignatureAlgorithm.RS256, hash).SignatureReturn resultEndFunction ''' <summary>''' Releases resources used by this object.''' </summary>PublicSubDispose() ImplementsIDisposable.Dispose_certificateClient = NothingIf_certificateIsNotNothingThen_certificate.Dispose()_certificate = NothingEndIf_cryptographyClient = NothingEndSubEndClass