public ActionResult Complete(BatchSignatureCompleteRequest request)
        {
            byte[] signatureContent;

            // Recover the "transfer data" content stored in a temporary file.
            byte[] transferDataContent;
            if (!StorageMock.TryGetFile(request.TransferDataFileId, out transferDataContent))
            {
                return(HttpNotFound());
            }

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

            // Set the signature policy.
            padesSigner.SetPolicy(GetSignaturePolicy());

            // Set the signature computed on the client-side, along with the "transfer data" recovered from a temporary file
            padesSigner.SetPreComputedSignature(request.Signature, transferDataContent);

            // 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();

            return(Json(new BatchSignatureCompleteResponse()
            {
                SignedFileId = StorageMock.Store(signatureContent, ".pdf")
            }));
        }
        private void completeSignature()
        {
            byte[] signatureContent;

            try {
                // Retrieve the "transfer data" stored on the initial step (see method startNextSignature()).
                var transferData = Storage.GetFile(TransferDataFileIdField.Value);

                // We won't be needing the "transfer data" anymore, so we delete it.
                Storage.DeleteFile(TransferDataFileIdField.Value);

                // 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(Convert.FromBase64String(SignatureField.Value), 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) {
                // One or more validations failed. We log the error and update the page with a summary of what
                // happened to this document.
                logger.Error(ex, "Validation error completing the signature of a batch document");
                setValidationError(ex.ValidationResults);
                return;
            }
            catch (Exception ex) {
                // An error has occurred. We log the error and update the page with a summary of what happened to
                // this document.
                logger.Error(ex, "Error completing the signature of a batch document");
                setError(ex.Message);
                return;
            }

            // Store the signed file.
            var file = Storage.StoreFile(signatureContent, ".pdf");

            // Update the page with a link to the signed file.
            var docItem = DocumentsListView.Items[DocumentIndex];

            docItem.DataItem = new DocumentItem()
            {
                Id           = DocumentIds[DocumentIndex],
                DownloadLink = "Download?file=" + file
            };
            docItem.DataBind();
        }
        public ActionResult Complete(SignatureCompleteModel model)
        {
            byte[] signatureContent;

            try {
                // Recover the "transfer data" content stored in a temporary file.
                byte[] transferDataContent;
                if (!StorageMock.TryGetFile(model.TransferDataFileId, out transferDataContent))
                {
                    return(HttpNotFound());
                }

                // Get an instance of the PadesSigner class.
                var padesSigner = new PadesSigner();

                // Set the signature policy.
                padesSigner.SetPolicy(GetSignaturePolicy());

                // Set the signature computed on the client-side, along with the "transfer data" (rendered in a hidden field, see the view)
                padesSigner.SetPreComputedSignature(model.Signature, transferDataContent);

                // 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.
                ModelState.AddModelError("", ex.ValidationResults.ToString());
                // Return userfile to continue the signature with the same file.
                return(View(model));
            }

            // On the next step (SignatureInfo action), we'll render the following information:
            // - The filename to be available to download in next action.
            // - The signer's certificate information to be rendered.
            // We'll store these values on TempData, which is a dictionary shared between actions.
            TempData["SignatureInfoModel"] = new SignatureInfoModel()
            {
                // Store the signature file on the folder "App_Data/" and redirects to the SignatureInfo action with the filename.
                // With this filename, it can show a link to download the signature file.
                File = StorageMock.Store(signatureContent, ".pdf")
            };

            return(RedirectToAction("SignatureInfo"));
        }
Ejemplo n.º 4
0
        protected void SubmitSignatureButton_Click(object sender, EventArgs e)
        {
            byte[] signatureContent;

            try {
                // Retrieve the "transfer data" stored on the initial step (see method startNextSignature()).
                var transferData = Storage.GetFile(TransferDataFileIdField.Value);

                // We won't be needing the "transfer data" anymore, so we delete it.
                Storage.DeleteFile(TransferDataFileIdField.Value);

                // 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(Convert.FromBase64String(SignatureField.Value), 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.
                ex.ValidationResults.Errors.ForEach(ve => ModelState.AddModelError("", ve.ToString()));
                CertificateField.Value = "";
                ToSignHashField.Value  = "";
                return;
            }

            // Pass the following fields to be used on PadesSignatureInfo page:
            // - The signature file will be stored on the folder "App_Data/". Its name will be passed by
            //   SignatureFile field.
            // - The user's certificate
            this.SignatureFile = Storage.StoreFile(signatureContent, ".pdf");
            this.Certificate   = PKCertificate.Decode(Convert.FromBase64String(CertificateField.Value));

            Server.Transfer("PadesSignatureInfo.aspx");
        }
        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));
        }
Ejemplo n.º 6
0
        public IHttpActionResult Complete(BatchSignatureCompleteRequest request)
        {
            byte[] signatureContent;

            try {
                // Recover the "transfer data" content stored in a temporary file
                string extension;
                byte[] transferDataContent;
                if (!Storage.TryGetFile(request.TransferDataFileId, out transferDataContent, out extension))
                {
                    return(NotFound());
                }

                // 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" recovered from a temporary file
                padesSigner.SetPreComputedSignature(request.Signature, transferDataContent);

                // 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.
                var message = Request.CreateErrorResponse(HttpStatusCode.InternalServerError, ex.ValidationResults.ToString());
                return(ResponseMessage(message));
            }

            return(Ok(new BatchSignatureCompleteResponse()
            {
                SignedFileId = Storage.StoreFile(signatureContent, ".pdf")
            }));
        }
        public ActionResult Complete(SignatureCompleteModel model)
        {
            byte[] signatureContent;

            try {
                // 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" (rendered in a hidden field, see the view)
                padesSigner.SetPreComputedSignature(model.Signature, model.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.
                ModelState.AddModelError("", ex.ValidationResults.ToString());
                return(View());
            }

            // On the next step (SignatureInfo action), we'll render the following information:]
            // - The filename to be available to download in next action.
            // - The signer's certificate information to be rendered.
            // We'll store these values on TempData, which is a dictionary shared between actions.
            TempData["SignatureInfoModel"] = new SignatureInfoModel()
            {
                // Store the signature file on the folder "App_Data/" and redirects to the SignatureInfo action with the filename.
                // With this filename, it can show a link to download the signature file.
                Filename = Storage.StoreFile(signatureContent, ".pdf"),
                UserCert = PKCertificate.Decode(model.CertContent)
            };

            return(RedirectToAction("SignatureInfo"));
        }