CertificateHolder Class |
Namespace: Aspose.Words
The CertificateHolder type exposes the following members.
Name | Description | |
---|---|---|
![]() ![]() | Certificate |
Returns the instance of X509Certificate2 which holds private, public keys and certificate chain.
|
Name | Description | |
---|---|---|
![]() ![]() ![]() | Create(Byte, SecureString) |
Creates CertificateHolder object using byte array of PKCS12 store and its password.
|
![]() ![]() ![]() | Create(Byte, String) |
Creates CertificateHolder object using byte array of PKCS12 store and its password.
|
![]() ![]() ![]() | Create(String, String) |
Creates CertificateHolder object using path to PKCS12 store and its password.
|
![]() ![]() ![]() | Create(String, String, String) |
Creates CertificateHolder object using path to PKCS12 store, its password and the alias by using which private key and certificate will be found.
|
![]() | Equals | (Inherited from Object.) |
![]() | GetHashCode | (Inherited from Object.) |
![]() | GetType | (Inherited from Object.) |
![]() | ToString | (Inherited from Object.) |
CertificateHolder can be created by static factory methods only. It contains an instance of X509Certificate2 which is used to introduce private, public keys and certificate chains into the system. This class is applied in DigitalSignatureUtil and PdfDigitalSignatureDetails instead of obsolete methods with X509Certificate2 as parameters.
CertificateHolder certificateHolder = CertificateHolder.Create(MyDir + "morzal.pfx", "aw"); SignOptions signOptions = new SignOptions { Comments = "My comment", SignTime = DateTime.Now }; using (Stream streamIn = new FileStream(MyDir + "Digitally signed.docx", FileMode.Open)) { using (Stream streamOut = new FileStream(ArtifactsDir + "DigitalSignatureUtil.SignDocument.docx", FileMode.OpenOrCreate)) { DigitalSignatureUtil.Sign(streamIn, streamOut, certificateHolder, signOptions); } }
// Create certificate holder from a file CertificateHolder certificateHolder = CertificateHolder.Create(MyDir + "morzal.pfx", "aw"); SignOptions signOptions = new SignOptions { Comments = "Comment", SignTime = DateTime.Now, DecryptionPassword = "docPassword" }; // Digitally sign encrypted with "docPassword" document in the specified path string inputFileName = MyDir + "Encrypted.docx"; string outputFileName = ArtifactsDir + "DigitalSignatureUtil.DecryptionPassword.docx"; DigitalSignatureUtil.Sign(inputFileName, outputFileName, certificateHolder, signOptions);
[Description("WORDSNET-16868")] public static void Sign() { string signPersonName = "Ron Williams"; string srcDocumentPath = MyDir + "Document.docx"; string dstDocumentPath = ArtifactsDir + "SignDocumentCustom.Sign.docx"; string certificatePath = MyDir + "morzal.pfx"; string certificatePassword = "aw"; // We need to create simple list with test signers for this example CreateSignPersonData(); Console.WriteLine("Test data successfully added!"); // Get sign person object by name of the person who must sign a document // This an example, in real use case you would return an object from a database SignPersonTestClass signPersonInfo = (from c in gSignPersonList where c.Name == signPersonName select c).FirstOrDefault(); if (signPersonInfo != null) { SignDocument(srcDocumentPath, dstDocumentPath, signPersonInfo, certificatePath, certificatePassword); Console.WriteLine("Document successfully signed!"); } else { Console.WriteLine("Sign person does not exist, please check your parameters."); } // Now do something with a signed document, for example, save it to your database // Use 'new Document(dstDocumentPath)' for loading a signed document } /// <summary> /// Signs the document obtained at the source location and saves it to the specified destination. /// </summary> private static void SignDocument(string srcDocumentPath, string dstDocumentPath, SignPersonTestClass signPersonInfo, string certificatePath, string certificatePassword) { // Create new document instance based on a test file that we need to sign Document document = new Document(srcDocumentPath); DocumentBuilder builder = new DocumentBuilder(document); // Add info about responsible person who sign a document SignatureLineOptions signatureLineOptions = new SignatureLineOptions { Signer = signPersonInfo.Name, SignerTitle = signPersonInfo.Position }; // Add signature line for responsible person who sign a document SignatureLine signatureLine = builder.InsertSignatureLine(signatureLineOptions).SignatureLine; signatureLine.Id = signPersonInfo.PersonId; // Save a document with line signatures into temporary file for future signing builder.Document.Save(dstDocumentPath); // Create holder of certificate instance based on your personal certificate // This is the test certificate generated for this example CertificateHolder certificateHolder = CertificateHolder.Create(certificatePath, certificatePassword); // Link our signature line with personal signature SignOptions signOptions = new SignOptions { SignatureLineId = signPersonInfo.PersonId, SignatureLineImage = signPersonInfo.Image }; // Sign a document which contains signature line with personal certificate DigitalSignatureUtil.Sign(dstDocumentPath, dstDocumentPath, certificateHolder, signOptions); } #if NETFRAMEWORK /// <summary> /// Converting image file to bytes array. /// </summary> private static byte[] ImageToByteArray(Image imageIn) { using (MemoryStream ms = new MemoryStream()) { imageIn.Save(ms, ImageFormat.Png); return ms.ToArray(); } } #endif /// <summary> /// Create test data that contains info about sing persons /// </summary> private static void CreateSignPersonData() { gSignPersonList = new List<SignPersonTestClass> { #if NETFRAMEWORK new SignPersonTestClass(Guid.NewGuid(), "Ron Williams", "Chief Executive Officer", ImageToByteArray(Image.FromFile(ImageDir + "Logo.jpg"))), #else new SignPersonTestClass(Guid.NewGuid(), "Ron Williams", "Chief Executive Officer", SkiaSharp.SKBitmap.Decode(ImageDir + "Logo.jpg").Bytes), #endif #if NETFRAMEWORK new SignPersonTestClass(Guid.NewGuid(), "Stephen Morse", "Head of Compliance", ImageToByteArray(Image.FromFile(ImageDir + "Logo.jpg"))) #else new SignPersonTestClass(Guid.NewGuid(), "Stephen Morse", "Head of Compliance", SkiaSharp.SKBitmap.Decode(ImageDir + "Logo.jpg").Bytes) #endif }; } private static List<SignPersonTestClass> gSignPersonList;