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") })); }
public ActionResult Complete(BatchSignatureCompleteRequest request) { // 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 = request.Token, // Set the result of the RSA signature. Notice that this call is not necessary on the "regular" batch signature example Signature = Convert.FromBase64String(request.Signature), }; // Call the Finish() method, which finalizes the signature process and returns a SignatureResult object var signatureResult = signatureFinisher.Finish(); // The "Certificate" property of the SignatureResult object contains information about the certificate used by the user // to sign the file. var signerCert = signatureResult.Certificate; // At this point, you'd typically store the signed PDF on your database. For demonstration purposes, we'll // store the PDF on the App_Data folder and render a page with a link to download the signed PDF and with the // signer's certificate details. var appDataPath = Server.MapPath("~/App_Data"); if (!Directory.Exists(appDataPath)) { Directory.CreateDirectory(appDataPath); } var signedFileId = Guid.NewGuid(); var filename = signedFileId + ".pdf"; // 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. signatureResult.WriteToFile(Path.Combine(appDataPath, filename)); var signedFile = filename.Replace(".", "_"); // Note: we're passing the filename argument with "." as "_" because of limitations of ASP.NET MVC return(Json(signedFile)); }
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 async Task <ActionResult> Complete(BatchSignatureCompleteRequest request) { // 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 = request.Token, // Set the result of the RSA signature. Notice that this call is not necessary on the // "regular" batch signature example. Signature = Convert.FromBase64String(request.Signature), }; // Call the FinishAsync() method, which finalizes the signature process and returns a // SignatureResult object. var result = await signatureFinisher.FinishAsync(); // The "Certificate" property of the SignatureResult object contains information about the // certificate used by the user to sign the file. var signerCert = result.Certificate; // 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"); } // Return a JSON with the signed file's id, stored using our mock class. (the page wil use // jQuery to decode this value) return(Json(fileId)); }
public ActionResult Complete(BatchSignatureCompleteRequest request) { // Get an instance of the PadesSignatureFinisher class, responsible for completing the signature process var signatureFinisher = new PadesSignatureFinisher(Util.GetRestPkiClient()) { // Set the token for this signature (rendered in a hidden input field, see the view) Token = request.Token, // Set the result of the RSA signature. Notice that this call is not necessary on the "regular" batch signature example Signature = request.Signature }; // Call the Finish() method, which finalizes the signature process and returns the signed PDF var signedPdf = signatureFinisher.Finish(); // Get information about the certificate used by the user to sign the file. This method must only be called after // calling the Finish() method. var signerCert = signatureFinisher.GetCertificateInfo(); // At this point, you'd typically store the signed PDF on your database. For demonstration purposes, we'll // store the PDF on the App_Data folder and render a page with a link to download the signed PDF and with the // signer's certificate details. var appDataPath = Server.MapPath("~/App_Data"); if (!Directory.Exists(appDataPath)) { Directory.CreateDirectory(appDataPath); } var signedFileId = Guid.NewGuid(); var filename = signedFileId + ".pdf"; System.IO.File.WriteAllBytes(Path.Combine(appDataPath, filename), signedPdf); var signedFile = filename.Replace(".", "_"); // Note: we're passing the filename argument with "." as "_" because of limitations of ASP.NET MVC return(Json(signedFile)); }