public IHttpActionResult Complete(SignatureCompleteRequest request)
        {
            byte[] signatureContent;

            try {
                // Instantiate a CadesSigner class
                var cadesSigner = new CadesSigner();

                // Set the document to be signed and the policy, exactly like in the Start action
                if (!string.IsNullOrEmpty(request.FileId))
                {
                    cadesSigner.SetDataToSign(Storage.GetFile(request.FileId));
                }
                else
                {
                    cadesSigner.SetDataToSign(Storage.GetSampleDocContent());
                }

                cadesSigner.SetPolicy(getSignaturePolicy());

                // Set signer's certificate
                cadesSigner.SetSigningCertificate(PKCertificate.Decode(request.Certificate));

                // Set the signature computed on the client-side, along with the "to-sign-bytes" received from the request.
                cadesSigner.SetPrecomputedSignature(request.Signature, request.ToSignBytes);

                // Call ComputeSignature(), which does all the work, including validation of the signer's certificate and of the resulting signature.
                cadesSigner.ComputeSignature();

                // Get the signature as an array of bytes
                signatureContent = cadesSigner.GetSignature();
            } catch (ValidationException ex) {
                // Some of the operations above may throw a ValidationException, for instance if the certificate is revoked.
                return(new ResponseMessageResult(Request.CreateResponse(HttpStatusCode.BadRequest, new ValidationErrorModel(ex.ValidationResults))));
            }

            // Pass the following fields to be used on signature-results template:
            // - The signature file will be stored on the folder "App_Data/". Its name will be passed by Filename field.
            // - The user's certificate
            var response = new SignatureCompleteResponse()
            {
                Filename    = Storage.StoreFile(signatureContent, ".p7s"),
                Certificate = new CertificateModel(PKCertificate.Decode(request.Certificate))
            };

            return(Ok(response));
        }
Пример #2
0
        public async Task <SignatureCompleteResponse> Complete(string token)
        {
            var storage = new Storage(hostingEnvironment);
            var client  = Util.GetRestPkiClient(restPkiConfig);

            // Get an instance of the PadesSignatureFinisher2 class, responsible for completing the signature process
            var signatureFinisher = new PadesSignatureFinisher2(client)
            {
                // Set the token for this signature (acquired previously and passed back here by the angular controller)
                Token = token
            };

            // Call the FinishAsync() method, which finalizes the signature process and returns a SignatureResult object
            var signatureResult = await signatureFinisher.FinishAsync();

            // The "Certificate" property of the SignatureResult object contains information about the certificate used by the user
            // to sign the file.
            var signerCert = signatureResult.Certificate;

            // At this point, you'd typically store the signed PDF on a database or storage service. For demonstration purposes, we'll
            // store the PDF on our "storage mock", which in turn stores the PDF on the App_Data folder.

            // The SignatureResult object has various methods for writing the signature file to a stream (WriteToAsync()), local file (WriteToFileAsync()),
            // open a stream to read the content (OpenReadAsync()) and get its contents (GetContentAsync()). Avoid the method GetContentAsync() to prevent
            // memory allocation issues with large files.
            string filename;

            using (var signatureStream = await signatureResult.OpenReadAsync()) {
                filename = await storage.StoreAsync(signatureStream, ".pdf");
            }

            // Pass the following fields to be used on signature-results template:
            // - The signature filename, which can be used to provide a link to the file
            // - The user's certificate
            var response = new SignatureCompleteResponse()
            {
                Filename    = filename,
                Certificate = new Models.CertificateModel(signerCert)
            };

            return(response);
        }
        public IHttpActionResult Complete(SignatureCompleteRequest request)
        {
            byte[] signatureContent;

            try {
                // Retrieve the "transfer data" stored on the initial step (see Start action)
                var transferData = Storage.GetFile(request.TransferDataFileId);

                // We won't be needing the "transfer data" anymore, so we delte it
                Storage.DeleteFile(request.TransferDataFileId);

                // Instantiate a PadesSigner class
                var padesSigner = new PadesSigner();

                // Set the signature policy, exactly like in the Start method
                padesSigner.SetPolicy(getSignaturePolicy());

                // Set the signature computed on the client-side, along with the "transfer data"
                padesSigner.SetPreComputedSignature(request.Signature, transferData);

                // Call ComputeSignature(), which does all the work, including validation of the signer's certificate and of the resulting signature
                padesSigner.ComputeSignature();

                // Get the signed PDF as an array of bytes
                signatureContent = padesSigner.GetPadesSignature();
            } catch (ValidationException ex) {
                // Some of the operations above may throw a ValidationException, for instance if the certificate is revoked.
                return(new ResponseMessageResult(Request.CreateResponse(HttpStatusCode.BadRequest, new ValidationErrorModel(ex.ValidationResults))));
            }

            // Pass the following fields to be used on signature-results template:
            // - The signature file will be stored on the folder "App_Data/". Its name will be passed by Filename field.
            // - The user's certificate
            var response = new SignatureCompleteResponse()
            {
                Filename    = Storage.StoreFile(signatureContent, ".pdf"),
                Certificate = new CertificateModel(PKCertificate.Decode(request.Certificate))
            };

            return(Ok(response));
        }
Пример #4
0
        public IHttpActionResult Complete(SignatureCompleteRequest request)
        {
            byte[] signatureContent;

            try {
                // Instantiate a XmlElementSigner class
                var signer = new XmlElementSigner();

                // Set the document to be signed and the policy, exactly like in the Start action
                signer.SetXml(Storage.GetSampleNFeContent());
                signer.SetPolicy(getSignaturePolicy());

                // Set the signature computed on the client-side, along with the "transfer data"
                signer.SetPrecomputedSignature(request.Signature, request.TransferData);

                // Call ComputeSignature(), which does all the work, including validation of the signer's certificate and of the resulting signature
                signer.ComputeSignature();

                // Get the signed XML as an array of bytes
                signatureContent = signer.GetSignedXml();
            } catch (ValidationException ex) {
                // Some of the operations above may throw a ValidationException, for instance if the certificate is revoked.
                return(new ResponseMessageResult(Request.CreateResponse(HttpStatusCode.BadRequest, new ValidationErrorModel(ex.ValidationResults))));
            }

            // Pass the following fields to be used on signature-results template:
            // - The signature file will be stored on the folder "App_Data/". Its name will be passed by Filename field.
            // - The user's certificate
            var response = new SignatureCompleteResponse()
            {
                Filename    = Storage.StoreFile(signatureContent, ".xml"),
                Certificate = new CertificateModel(PKCertificate.Decode(request.Certificate))
            };

            return(Ok(response));
        }