Click here to Skip to main content
15,867,885 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
hi everybody, i'm trying to develope a web application to be able sign a pdf document, so i need the client digital signature to sign it, i found a lot of code that describe to do this, but not something clear for a web application,i would like to someone help me how can to do that, thanks in advance...

attached the code that i found

What I have tried:

C#
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();
            }
        }

    }
}
Posted
Comments
Dave Kreskowiak 10-Jun-16 23:04pm    
You want to sign a PDF document on the server with a client-side certificate? Not going to happen. Your code has no access to the client certificate at all.

All ASP.NET code runs entirely on the server, never the client.
Member 12179018 23-Jun-16 10:55am    
So, you think there´s no way to do it ?
[no name] 26-Aug-19 9:20am    
Please refer to answer at https://stackoverflow.com/a/55676351/9659885
It points to complete working project along with source code.
bmarcco 30-Sep-22 8:02am    
Hi Member 12179018. Did you manage to solve it, I have the same problem ?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900