protected void SubmitButton_Click(object sender, EventArgs e) { // Get an instance of the CadesSignatureFinisher2 class, responsible for completing the signature process var signatureFinisher = new CadesSignatureFinisher2(Util.GetRestPkiClient()) { // Set the token for this signature acquired previously Token = (string)ViewState["Token"] }; // Call the Finish() method, which finalizes the signature process and returns a SignatureResult object var result = signatureFinisher.Finish(); // At this point, you'd typically store the signed PDF on your database. For demonstration purposes, we'll // store the PDF on our mock Storage class. // The SignatureResult object has various methods for writing the signature file to a stream (WriteTo()), local file (WriteToFile()), open // a stream to read the content (OpenRead()) and get its contents (GetContent()). For large files, avoid the method GetContent() to avoid // memory allocation issues. string fileId; using (var resultStream = result.OpenRead()) { fileId = StorageMock.Store(resultStream, ".p7s"); } // If you prefer a simpler approach without streams, simply do: //fileId = StorageMock.Store(result.GetContent(), ".pdf"); // What you do at this point is up to you. For demonstration purposes, we'll render a page with a link to // download the signed PDF and with the signer's certificate details. this.SignatureFilename = fileId; this.SignerCertificate = result.Certificate; Server.Transfer("CadesSignatureInfo.aspx"); }
public static string Complete(string token) { // Get an instance of the PadesSignatureFinisher2 class, responsible for completing the signature process var signatureFinisher = new PadesSignatureFinisher2(Util.GetRestPkiClient()) { // Set the token for this signature acquired previously Token = token }; // Call the Finish() method, which finalizes the signature process and returns an SignatureResult object to access // the signed PDF var result = signatureFinisher.Finish(); // At this point, you'd typically store the signed PDF on your database. For demonstration purposes, we'll // store the PDF on our mock Storage class. // The SignatureResult object has various methods for writing the signature file to a stream (WriteTo()), local file (WriteToFile()), open // a stream to read the content (OpenRead()) and get its contents (GetContent()). For large files, avoid the method GetContent() to avoid // memory allocation issues. string fileId; using (var resultStream = result.OpenRead()) { fileId = StorageMock.Store(resultStream, ".pdf"); } // If you prefer a simpler approach without streams, simply do: //fileId = StorageMock.Store(result.GetContent(), ".pdf"); // Send to the javascript the signed file's id to be referenced on a download link return(fileId); }
// You may also change texts, positions and more by editing directly the method generatePrinterFriendlyVersion below // ################################################################################################################## protected void Page_Load(object sender, EventArgs e) { // Get document ID from query string var fileId = Request.QueryString["file"]; // Locate document and read content from storage var fileContent = StorageMock.Read(fileId); // Check if doc already has a verification code registered on storage var verificationCode = StorageMock.GetVerificationCode(fileId); if (verificationCode == null) { // If not, generate a code and register it verificationCode = Util.GenerateVerificationCode(); StorageMock.SetVerificationCode(fileId, verificationCode); } // Generate the printer-friendly version var pfvContent = generatePrinterFriendlyVersion(fileContent, verificationCode); // Return printer-friendly version as a downloadable file Response.ContentType = "application/pdf"; Response.AddHeader("Content-Disposition", "attachment; filename=printer-friendly.pdf"); Response.BinaryWrite(pfvContent); Response.End(); }
protected void SubmitButton_Click(object sender, EventArgs e) { // Get an instance of the PadesSignatureFinisher2 class, responsible for completing the signature process var signatureFinisher = new PadesSignatureFinisher2(Util.GetRestPkiClient()) { // Set the token for this signature (rendered in a hidden input field, see the view) Token = (string)ViewState["Token"] }; // Call the Finish() method, which finalizes the signature process and returns an object to access the signed PDF var result = signatureFinisher.Finish(); // At this point, you'd typically store the signed PDF on your database. For demonstration purposes, we'll // store the PDF on our mock Storage class string fileId; using (var resultStream = result.OpenRead()) { fileId = StorageMock.Store(resultStream, ".pdf"); } // If you prefer a simpler approach without streams, simply do: // fileId = Storage.Store(result.GetContent(), ".pdf"); // What you do at this point is up to you. For demonstration purposes, we'll render a page with a link to // download the signed PDF and with the signer's certificate details. this.SignatureFilename = fileId; this.SignerCertificate = result.Certificate; Server.Transfer("PadesSignatureInfo.aspx"); }
protected void Page_Load(object sender, EventArgs e) { if (!string.IsNullOrEmpty(Request.QueryString["fileId"])) { try { var filename = Request.QueryString["fileId"]; using (var fileStream = StorageMock.OpenRead(filename)) { Response.ContentType = MimeMapping.GetMimeMapping(filename); Response.AddHeader("Content-Disposition", "attachment; filename=" + filename); fileStream.CopyTo(Response.OutputStream); } } catch (FileNotFoundException ex) { Response.StatusCode = 404; } } else { string path = null; string filename = null; if (!string.IsNullOrEmpty(Request.QueryString["file"])) { switch (Request.QueryString["file"]) { case "SampleDocument": path = Util.GetSampleDocPath(); filename = "SampleDocument.pdf"; break; case "SampleNFe": path = Util.GetSampleNFePath(); filename = "SampleNFe.xml"; break; } } else if (!string.IsNullOrEmpty(Request.QueryString["docId"])) { int docId; if (int.TryParse(Request.QueryString["docId"], out docId)) { path = Util.GetBatchDocPath(docId); filename = string.Format("{0:D2}.pdf", docId); } } if (path != null) { Response.ContentType = MimeMapping.GetMimeMapping(filename); Response.AddHeader("Content-Disposition", "attachment; filename=" + filename); Response.WriteFile(path); } else { Response.StatusCode = 404; } } Response.End(); }
private void completeSignature() { string fileId; try { // Get an instance of the PadesSignatureFinisher2 class, responsible for completing the signature process var signatureFinisher = new PadesSignatureFinisher2(Util.GetRestPkiClient()) { // Retrieve the token for this signature stored as hidden field on the initial step (see method startNextSignature()) Token = TokenField.Value }; // Call the Finish() method, which finalizes the signature process and returns an SignatureResult object to access // the signed PDF var result = signatureFinisher.Finish(); // At this point, you'd typically store the signed PDF on your database. For demonstration purposes, we'll // store the PDF on our mock Storage class. // The SignatureResult object has various methods for writing the signature file to a stream (WriteTo()), local file (WriteToFile()), open // a stream to read the content (OpenRead()) and get its contents (GetContent()). For large files, avoid the method GetContent() to avoid // memory allocation issues. using (var resultStream = result.OpenRead()) { fileId = StorageMock.Store(resultStream, ".pdf"); } // If you prefer a simpler approach without streams, simply do: //fileId = StorageMock.Store(result.GetContent(), ".pdf"); } 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 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 setError(ex.Message); return; } // Update the page with a link to the signed file var docItem = DocumentsListView.Items[DocumentIndex]; docItem.DataItem = new DocumentItem() { Id = DocumentIds[DocumentIndex], DownloadLink = "Download.aspx?fileId=" + fileId }; docItem.DataBind(); }
protected void Page_Load(object sender, EventArgs e) { // Get verification code from query string var formattedVerificationCode = Request.QueryString["code"]; // On PrinterFriendlyVersion.aspx, we stored the unformatted version of the verification code (without hyphens) but // used the formatted version (with hyphens) on the printer-friendly PDF. Now, we remove the hyphens before looking it up. var verificationCode = AlphaCode.Parse(formattedVerificationCode); // Get document associated with verification code var fileId = StorageMock.LookupVerificationCode(verificationCode); if (fileId == null) { // Invalid code given! // Small delay to slow down brute-force attacks (if you want to be extra careful you might want to add a CAPTCHA to the process) Thread.Sleep(TimeSpan.FromSeconds(2)); // Return Not Found Response.StatusCode = 404; Response.End(); return; } // Read document from storage var fileContent = StorageMock.Read(fileId); // Open and validate signatures with Rest PKI var client = Util.GetRestPkiClient(); var sigExplorer = new PadesSignatureExplorer(client) { Validate = true, DefaultSignaturePolicyId = StandardPadesSignaturePolicies.Basic, SecurityContextId = Util.GetSecurityContextId(), }; sigExplorer.SetSignatureFile(fileContent); var signature = sigExplorer.Open(); // Set properties for rendering on page (see aspx file) this.FileId = fileId; this.Model = signature; }