Ejemplo n.º 1
0
        public async Task <ActionResult> Index(string userfile)
        {
            // Our action only works if a userfile is given to work with.
            if (!StorageMock.TryGetFile(userfile, out string userfilePath))
            {
                return(HttpNotFound());
            }

            // Get an instance of the PadesSignatureExplorer class, used to open/validate PDF signatures.
            var sigExplorer = new PadesSignatureExplorer(Util.GetRestPkiClient())
            {
                // Specify that we want to validate the signatures in the file, not only inspect them.
                Validate = true,
                // Specify the parameters for the signature validation:
                // Accept any PAdES signature as long as the signer has an ICP-Brasil certificate.
                DefaultSignaturePolicyId = StandardPadesSignaturePolicies.Basic,
                // Specify 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 the PAdES signature file.
            sigExplorer.SetSignatureFile(userfilePath);

            // Call the Open() method, which returns the signature file's information.
            var signature = await sigExplorer.OpenAsync();

            // Render the information (see file OpenPadesSignature/Index.html for more information on
            // the information returned).
            return(View(new OpenPadesSignatureModel()
            {
                Signature = signature
            }));
        }
Ejemplo n.º 2
0
        public async Task <ActionResult> Index(string userfile)
        {
            // Get an instance of the PadesSignatureExplorer class, used to open/validate PDF signatures.
            var sigExplorer = new PadesSignatureExplorer(Util.GetRestPkiClient())
            {
                // Specify that we want an audit package to be generated, and that the signed file should be
                // included in the package.
                GenerateAuditPackage            = true,
                IncludeSignedFileInAuditPackage = true,
                // In order to generate an audit package, we must also pass Validate = true.
                Validate = true,
                // Specify the parameters for the signature validation:
                // Accept any PAdES signature as long as the signer has an ICP-Brasil certificate.
                DefaultSignaturePolicyId = StandardPadesSignaturePolicies.Basic,
                // We have encapsulated the security context choice on Util.cs.
                SecurityContextId = Util.GetSecurityContextId()
            };

            // Set the PDF file.
            if (string.IsNullOrEmpty(userfile))
            {
                // If no file is passed, we use a previously signed and B-Stamped file.
                sigExplorer.SetSignatureFile(Server.MapPath("~/Content/bstamped.pdf"));
            }
            else
            {
                sigExplorer.SetSignatureFile(Server.MapPath("~/App_Data/" + userfile.Replace("_", ".")));
                // Note: we're receiving the userfile argument with "_" as "." because of limitations of
                // ASP.NET MVC.
            }

            // Call the OpenAsync() method, which returns the signature file's information.
            var signature = await sigExplorer.OpenAsync();

            // If the document has been B-Stamped, store the "digest index file" to show a link on the page.
            if (signature.BStamp != null)
            {
                string indexFileId;
                using (var indexFileStream = signature.BStamp.IndexFile.OpenRead()) {
                    indexFileId = StorageMock.Store(indexFileStream, ".txt");
                }
                ViewBag.BStampIndexFile = indexFileId;
            }

            // Store the generated audit package. Notice that although we asked for its generation, the
            // signature might not have been B-Stamped yet, so an audit package might not be returned.
            if (signature.AuditPackage != null)
            {
                string auditPkgId;
                using (var auditPkgStream = signature.AuditPackage.OpenRead()) {
                    auditPkgId = StorageMock.Store(auditPkgStream, ".zip");
                }
                ViewBag.AuditPackageFile = auditPkgId;
            }

            // Render the information. (see file Views/OpenPadesSignatureBStamp/Index.html for more
            // information on the information returned)
            return(View(signature));
        }
        public async Task <ActionResult> Index(string userfile)
        {
            // Get an instance of the PadesSignatureExplorer class, used to open/validate PDF signatures.
            var sigExplorer = new PadesSignatureExplorer(Util.GetRestPkiClient())
            {
                // Specify that we want an audit package to be generated, and that the signed file should be
                // included in the package.
                GenerateAuditPackage            = true,
                IncludeSignedFileInAuditPackage = true,
                // In order to generate an audit package, we must also pass Validate = true.
                Validate = true,
                // Specify the parameters for the signature validation:
                // Accept any PAdES signature as long as the signer has an ICP-Brasil certificate.
                DefaultSignaturePolicyId = StandardPadesSignaturePolicies.Basic,
                // Specify 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 the PDF file.
            if (!string.IsNullOrEmpty(userfile))
            {
                // Verify if the provided userfile exits and get its absolute path.
                string userfilePath;
                if (!StorageMock.TryGetFile(userfile, out userfilePath))
                {
                    return(HttpNotFound());
                }

                // Set the userfile's absolute path to the PadesSignatureExplorer instance.
                sigExplorer.SetSignatureFile(userfilePath);
            }
            else
            {
                // If no userfile is passed, we use a previously signed and B-Stamped file.
                sigExplorer.SetSignatureFile(StorageMock.GetSampleBStampedPath());
            }

            // Call the Open() method, which returns the signature file's information.
            var signature = await sigExplorer.OpenAsync();

            // If the document has been B-Stamped, store the "digest index file" to show a link on the page.
            string indexFileId = null;

            if (signature.BStamp != null)
            {
                using (var indexFileStream = signature.BStamp.IndexFile.OpenRead()) {
                    indexFileId = StorageMock.Store(indexFileStream, ".txt");
                }
            }

            // Store the generated audit package. Notice that although we asked for its generation, the
            // signature might not have been B-Stamped yet, so an audit package might not be returned.
            string auditPkgId = null;

            if (signature.AuditPackage != null)
            {
                using (var auditPkgStream = signature.AuditPackage.OpenRead()) {
                    auditPkgId = StorageMock.Store(auditPkgStream, ".zip");
                }
            }

            // Render the information (see file OpenPadesSignatureBStamp/Index.html for more
            // information on the information returned).
            return(View(new OpenPadesSignatureBStampModel()
            {
                Signature = signature,
                BStampIndexFile = indexFileId,
                AuditPackageFile = auditPkgId
            }));
        }