public ActionResult Index(SignatureStartModel model) { byte[] toSignHash, transferData; SignatureAlgorithm signatureAlg; try { // Instantiate a XmlSigner class var signer = new FullXmlSigner(); // Set the data to sign, which in the case of this example is a fixed sample document signer.SetXml(StorageMock.GetSampleXmlDocumentContent()); // Decode the user's certificate and set as the signer certificate signer.SetSigningCertificate(PKCertificate.Decode(model.CertContent)); // Set the signature policy signer.SetPolicy(getSignaturePolicy()); // Generate the "to-sign-hash". This method also yields the signature algorithm that must // be used on the client-side, based on the signature policy. toSignHash = signer.GenerateToSignHash(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. ModelState.AddModelError("", ex.ValidationResults.ToString()); return(View()); } // On the next step (Complete action), we'll need once again some information: // - The thumpprint of the selected certificate. // - The "transfer data" used to validate the signature in complete action.Its content is stored in // a temporary file (with extension .bin) to be shared with the Complete action. // - The "to-sign-hash" to be signed. (see signature-complete-form.js) // - The OID of the digest algorithm to be used during the signature operation. // We'll store this value on TempData, that will store in dictionary shared between actions. TempData["SignatureCompleteModel"] = new SignatureCompleteModel() { CertThumb = model.CertThumb, TransferDataFileId = StorageMock.Store(transferData, ".bin"), ToSignHash = toSignHash, DigestAlgorithmOid = signatureAlg.DigestAlgorithm.Oid }; return(RedirectToAction("Complete")); }
// GET: XmlServerKeySdk public ActionResult Index() { byte[] signatureContent; PKCertificateWithKey certWithKey; try { // Instantiate a XmlSigner class var signer = new FullXmlSigner(); // Set the data to sign, which in the case of this example is a fixed sample document signer.SetXml(StorageMock.GetSampleXmlDocumentContent()); // Get the server's certificate, stores it and and set as the signer certificate. var certContent = StorageMock.GetServerCertificate(); var store = Pkcs12CertificateStore.Load(certContent, "1234"); certWithKey = store.GetCertificatesWithKey().First(); signer.SetSigningCertificate(certWithKey); // Set the signature policy signer.SetPolicy(getSignaturePolicy()); // 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 // encoding cannot be read or if the certificate is expired. ModelState.AddModelError("", ex.ValidationResults.ToString()); return(View()); } return(View(new SignatureInfoModel() { // Store the signature file on the folder "App_Data/". // With this filename, it can show a link to download the signature file. File = StorageMock.Store(signatureContent, ".pdf"), SignerCertificate = certWithKey.Certificate })); }