public ActionResult SignCodComplete(SignatureCompleteModel model) { byte[] signatureContent; try { var signer = new XmlElementSigner(); // Set the document to be signed and the policy, exactly like in the SignCod method signer.SetXml(Storage.GetSampleCodEnvelope()); signer.SetPolicy(getSignaturePolicy()); // Set the signature computed on the client-side, along with the "transfer data" signer.SetPrecomputedSignature(model.Signature, model.TransferData); // It is not necessary to set the signing certificate nor the element ID to be signed, both are contained in the "transfer data" // Call ComputeSignature(), which validates the signature of the "to-sign-hash" and finishes the signature process 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. ModelState.AddModelError("", ex.ValidationResults.ToString()); return(View()); } // Store the signature file on the folder "App_Data/" and redirect to the SignCodResult action with the filename. var filename = Storage.StoreFile(signatureContent, ".xml"); return(RedirectToAction("SignCodResult", new SignatureInfoModel() { Filename = filename })); }
public ActionResult SignCodehComplete(string id, SignatureCompleteModel model) { // Recover XML envelope with signed COD element from "storage" based on its ID byte[] content; if (!StorageMock.TryGetFile(id, out content)) { return(HttpNotFound()); } byte[] signatureContent; try { // Recover the "transfer data" content stored in a temporary file. byte[] transferDataContent; if (!StorageMock.TryGetFile(model.TransferDataFileId, out transferDataContent)) { return(HttpNotFound()); } // Get an instance of the XmlElementSigner class. var signer = new XmlElementSigner(); // Set the document to be signed and the policy, exactly like in the SignCodeh method signer.SetXml(content); signer.SetPolicy(getSignaturePolicy()); // Set the signature computed on the client-side, along with the "transfer data" signer.SetPrecomputedSignature(model.Signature, transferDataContent); // Call ComputeSignature(), which validates the signature of the "to-sign-hash" and finishes the signature process 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. ModelState.AddModelError("", ex.ValidationResults.ToString()); return(View()); } // On the next step (SignatureInfo action), we'll render the following information: // - The filename to be available to download in next action. // We'll store these values on TempData, which is a dictionary shared between actions. TempData["SignatureInfoModel"] = new SignatureInfoModel() { // Store the signature file on the folder "App_Data/" and redirect to the SignCodehResult action with the filename. File = StorageMock.Store(signatureContent, ".xml") }; return(RedirectToAction("SignCodehSignatureInfo")); }
public ActionResult Complete(SignatureCompleteModel model) { byte[] signatureContent; try { // Recover the "transfer data" content stored in a temporary file. byte[] transferDataContent; if (!StorageMock.TryGetFile(model.TransferDataFileId, out transferDataContent)) { return(HttpNotFound()); } // Instantiate a XmlElementSigner class var signer = new XmlElementSigner(); // Set the document to be signed and the policy, exactly like in the Start method signer.SetXml(StorageMock.GetSampleNFeContent()); signer.SetPolicy(getSignaturePolicy()); // Set the signature computed on the client-side, along with the "transfer data" (rendered in a hidden field, see the view) signer.SetPrecomputedSignature(model.Signature, transferDataContent); // 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. ModelState.AddModelError("", ex.ValidationResults.ToString()); return(View()); } // On the next step (SignatureInfo action), we'll render the following information:] // - The filename to be available to download in next action. // - The signer's certificate information to be rendered. // We'll store these values on TempData, which is a dictionary shared between actions. TempData["SignatureInfoModel"] = new SignatureInfoModel() { // Store the signature file on the folder "App_Data/" and redirects to the SignatureInfo action with the filename. // With this filename, it can show a link to download the signature file. File = StorageMock.Store(signatureContent, ".xml") }; return(RedirectToAction("SignatureInfo")); }
protected void SubmitSignatureButton_Click(object sender, EventArgs e) { byte[] signatureContent; try { // Instantiate a XmlElementSigner class. var signer = new XmlElementSigner(); // Set the document to be signed and the policy, exactly like in the previous event // (SubmitCertificateButton_Click). signer.SetXml(Storage.GetSampleNFeContent()); signer.SetPolicy(getSignaturePolicy()); // Set the signature computed on the client-side, along with the "transfer data". signer.SetPrecomputedSignature(Convert.FromBase64String(SignatureField.Value), Convert.FromBase64String(TransferDataField.Value)); // 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. ex.ValidationResults.Errors.ForEach(ve => ModelState.AddModelError("", ve.ToString())); CertificateField.Value = ""; ToSignHashField.Value = ""; return; } // Pass the following fields to be used on XmlElementSignatureInfo page: // - The signature file will be stored on the folder "App_Data/". Its name will be passed by // SignatureFile field. // - The user's certificate this.SignatureFile = Storage.StoreFile(signatureContent, ".xml"); this.Certificate = PKCertificate.Decode(Convert.FromBase64String(CertificateField.Value)); Server.Transfer("XmlElementSignatureInfo.aspx"); }
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)); }