public ActionResult Start(BatchSignatureStartRequest request)
        {
            // Instantiate a CadesSigner class
            var cadesSigner = new CadesSigner();

            // Get the file's content.
            if (!StorageMock.TryGetFile(StorageMock.GetBatchDocPath(request.Id), out byte[] fileContent))
Пример #2
0
        public async Task <ActionResult> Start(int id)
        {
            // Get an instance of the CadesSignatureStarter class, responsible for receiving the signature
            // elements and start the signature process.
            var signatureStarter = new CadesSignatureStarter(Util.GetRestPkiClient())
            {
                // Set the signature policy.
                SignaturePolicyId = StandardCadesSignaturePolicies.PkiBrazil.AdrBasica,

                // Set the security context to be used to determine trust in the certificate chain. We have
                // encapsulated the security context choice on Util.cs.
                SecurityContextId = Util.GetSecurityContextId(),

                // Optionally, set whether the content should be encapsulated in the resulting CMS.
                EncapsulateContent = true
            };

            // Set the document to be signed based on its ID (passed to us from the page).
            signatureStarter.SetFileToSign(StorageMock.GetBatchDocPath(id));

            // Call the StartWithWebPki() method, which initiates the signature. This yields the token, a
            // 43-character case-sensitive URL-safe string, which identifies this signature process. We'll
            // use this value to call the signWithRestPki() method on the Web PKI component (see
            // batch-signature-form.js) and also to complete the signature on the POST action below (this
            // should not be mistaken with the API access token).
            var token = await signatureStarter.StartWithWebPkiAsync();

            // Return a JSON with the token obtained from REST PKI. (the page will use jQuery to decode this
            // value)
            return(Json(token));
        }
        public async Task <ActionResult> Start(int id)
        {
            // Get an instance of the PadesSignatureStarter class, responsible for receiving the signature
            // elements and start the signature process.
            var signatureStarter = new PadesSignatureStarter(Util.GetRestPkiClient())
            {
                // Set the unit of measurement used to edit the pdf marks and visual representations.
                MeasurementUnits = PadesMeasurementUnits.Centimeters,

                // Set the signature policy.
                SignaturePolicyId = StandardPadesSignaturePolicies.Basic,

                // Set the security context to be used to determine trust in the certificate chain. We have
                // encapsulated the security context choice on Util.cs.
                SecurityContextId = Util.GetSecurityContextId(),

                // Set a visual representation for the signature.
                VisualRepresentation = PadesVisualElements.GetVisualRepresentationForRestPki()
            };

            // Set the document to be signed based on its ID (passed to us from the page).
            signatureStarter.SetPdfToSign(StorageMock.GetBatchDocPath(id));

            /*
             *      Optionally, add marks to the PDF before signing. These differ from the signature visual
             *      representation in that they are actually changes done to the document prior to signing, not
             *      binded to any signature. Therefore, any number of marks can be added, for instance one per
             *      page, whereas there can only be one visual representation per signature. However, since the
             *      marks are in reality changes to the PDF, they can only be added to documents which have no
             *      previous signatures, otherwise such signatures would be made invalid by the changes to the
             *      document (see property PadesSignatureStarter.BypassMarksIfSigned). This problem does not
             *      occurr with signature visual representations.
             *
             *      We have encapsulated this code in a method to include several possibilities depending on the
             *      argument passed. Experiment changing the argument to see different examples of PDF marks.
             *      Once you decide which is best for your case, you can place the code directly here.
             */
            //signatureStarter.PdfMarks.Add(PadesVisualElements.GetPdfMark(1));

            // Call the StartWithWebPki() method, which initiates the signature. This yields the token,
            // a 43-character case-sensitive URL-safe string, which identifies this signature process. We'll
            // use this value to call the signWithRestPki() method on the Web PKI component (see
            // batch-signature-form.js) and also to complete the signature on the POST action below (this
            // should not be mistaken with the API access token).
            var token = await signatureStarter.StartWithWebPkiAsync();

            // Return a JSON with the token obtained from REST PKI. (the page will use jQuery to decode this
            // value)
            return(Json(token));
        }
        public ActionResult Start(BatchSignatureStartRequest request)
        {
            byte[]             toSignBytes, transferData;
            SignatureAlgorithm signatureAlg;

            try {
                // Decode the user's certificate
                var cert = PKCertificate.Decode(request.CertContent);

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

                // Set the PDF to sign, which in the case of this example is one of the batch documents
                padesSigner.SetPdfToSign(StorageMock.GetBatchDocPath(request.Id));

                // Set the signer certificate
                padesSigner.SetSigningCertificate(cert);

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

                // Set a visual representation for the signature.
                padesSigner.SetVisualRepresentation(PadesVisualElements.GetVisualRepresentationForPkiSdk(cert));

                // Generate the "to-sign-bytes". This method also yields the signature algorithm that must
                // be used on the client-side, based on the signature policy, as well as the "transfer data",
                // a byte-array that will be needed on the next step.
                toSignBytes = padesSigner.GetToSignBytes(out signatureAlg, out transferData);
            } catch (ValidationException ex) {
                // Some of the operations above may throw a ValidationException, for instance if the certificate
                // encoding cannot be read or if the certificate is expired.
                return(new HttpStatusCodeResult(500, ex.ValidationResults.ToString()));
            }

            // For the next steps, we'll need once again some information:
            // - The "transfer data" filename. Its content is stored in a temporary file (with extension .bin) to
            // be shared with the Complete action.
            // - The "to-sign-hash" (digest of the "to-sign-bytes"). And the OID of the digest algorithm to be
            // used during the signature operation. this information is need in the signature computation with
            // Web PKI component. (see batch-signature-form.js)
            return(Json(new BatchSignatureStartResponse()
            {
                TransferDataFileId = StorageMock.Store(transferData, ".bin"),
                ToSignHash = signatureAlg.DigestAlgorithm.ComputeHash(toSignBytes),
                DigestAlgorithmOid = signatureAlg.DigestAlgorithm.Oid
            }));
        }
        public ActionResult Doc(int id)
        {
            var fileContent = StorageMock.Read(StorageMock.GetBatchDocPath(id));

            return(File(fileContent, "application/pdf", string.Format("Doc{0:D2}.pdf", id)));
        }