using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Security; using iTextSharp.text.pdf; using bcrypto = Org.BouncyCastle.X509; using iTextSharp.text.pdf.security; using System.IO; using System.Security.Cryptography; using System.Security.Cryptography.X509Certificates; using System.Net; using System.Net.Http; using System.Web.Routing; namespace WebApplication2.Models { public class Certificado { private X509Certificate2 certificado; private X509Store st = new X509Store(StoreName.My, StoreLocation.CurrentUser); object locationCertificate = @"C:\certificado.cer"; private void Open() { st.Open(OpenFlags.ReadOnly); } private void Close() { st.Close(); } private Org.BouncyCastle.X509.X509Certificate[] get_chain(Org.BouncyCastle.X509.X509CertificateParser cp) { Org.BouncyCastle.X509.X509Certificate[] chain1 = new Org.BouncyCastle.X509.X509Certificate[] { cp.ReadCertificate(certificado.RawData) }; return chain1; } private void getCollection() { // Note: // If we want to use the client cert in an ASP.NET app, we may use something like this instead: // X509Certificate2 cert = new X509Certificate2(Request.ClientCertificate.Certificate); // Get its associated CSP and public key // X509Certificate2 clientCertInRequest = RequestContext.ClientCertificate; X509Certificate2Collection collection = st.Certificates.Find(X509FindType.FindBySubjectName, locationCertificate, false); // X509Certificate2Collection collection = X509Certificate2UI.SelectFromCollection(st.Certificates, "Seleccione su certificado:", "", X509SelectionFlag.SingleSelection); if (collection.Count > 0) { certificado = collection[0]; } } public string documento_firmado() { try { Open(); getCollection(); Close(); Org.BouncyCastle.X509.X509CertificateParser cp = new Org.BouncyCastle.X509.X509CertificateParser(); Org.BouncyCastle.X509.X509Certificate[] chain = get_chain(cp); Firmar firmar = new Firmar(@"C:\doc.pdf", @"C:\nuevo.pdf"); return firmar.firmar_pdf(certificado, chain); } catch (CryptographicException exc) { return exc.ToString(); } catch (Exception exc1) { return exc1.ToString(); } } } }
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)