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));
        }
        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.º 3
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));
        }
        public IHttpActionResult Complete(SignatureCompleteRequest model)
        {
            // We'll use the SignatureProcess ID to locate the values we stored during the signature first step
            SignatureProcess signatureProcess;
            using (var dbContext = new DbContext()) {
                signatureProcess = dbContext.SignatureProcesses.FirstOrDefault(p => p.Id == model.ProcessId);
                // We won't be needing this information again, so let's do some housekeeping
                if (signatureProcess != null) {
                    dbContext.SignatureProcesses.Remove(signatureProcess);
                    dbContext.SaveChanges();
                }
            }

            // If we haven't found the SignatureProcess, something went wrong and we cannot continue (this shouldn't normally happen)
            if (signatureProcess == null) {
                return NotFound();
            }

            byte[] signatureContent;
            try {

                var cadesSigner = new CadesSigner();

                // Set the document to be signed and the policy, exactly like in the Start method
                cadesSigner.SetDataToSign(Util.GetSampleDocContent());
                cadesSigner.SetPolicy(getPolicy());

                // Set signer's certificate recovered from the database
                cadesSigner.SetSigningCertificate(PKCertificate.Decode(signatureProcess.CadesSignerCertificate));

                // Set the signature computed on the client-side, along with the "to-sign-bytes" recovered from the database
                cadesSigner.SetPrecomputedSignature(model.Signature, signatureProcess.CadesToSign);

                // 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 Ok(new SignatureCompleteResponse() {
                    Success = false,
                    Message = "A validation error has occurred",
                    ValidationResults = ex.ValidationResults.ToString()
                });
            }

            // Store the signature for future download (see method SignatureController.Download in the Controllers folder)
            Signature signature;
            using (var dbContext = new DbContext()) {
                signature = Signature.Create();
                signature.Type = SignatureTypes.Cades;
                signature.Content = signatureContent;
                dbContext.Signatures.Add(signature);
                dbContext.SaveChanges();
            }

            // Inform the page of the success, along with the ID of the stored signature, so that the page
            // can render the download link
            return Ok(new SignatureCompleteResponse() {
                Success = true,
                SignatureId = signature.Id
            });
        }