Click here to Skip to main content
15,888,243 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
We have a Web Forms application made with Activix and itextsharp.dll, framework 4.5.1, signing a PDF document when squeegee the application on my machine works or application runs on the server and access the server remotely also works, however when I publish it Windows Server and access the application via the link only works until the time that the application requests the client's browser certificate but when will display the box to enter the password (pin) token (A3 - USB) does not show. In AClass.cs class has the Itextsharp.dll method that calls the Pin - MakeSignature.SignDetached ();
They can help me? follow what I got so far.
Note: If you want to test is the need to generate a dll AClass.cs class and references the itextsharp.dll do from the command line, we need to put the "itextsharp.dll" and "AClass.cs" class in the folder that the executable "CSC", follows the code.

( \Windows\Microsoft.NET\Framework\v4.0.xxxxx
csc /t:library AClass.cs /r:itextsharp.dll
regasm AClass.dll /tlb /codebase )


<html>
<head runat="server">
<title></title>
<script language="javascript">

function assinar() {

if (confirm("Do you want to confirm this operation?")) {
var x = new ActiveXObject("ANamespace.AClass");
x.Assinador(document.getElementById("hdfPath").value);
return true;
} else {
return false;
}
}
</script>

</head>
<body>
<form id="form1" runat="server">

Criar Documento:


Criar Documento


Assinar


Download

</form>
</body>
</html>


using iTextSharp.text;
using iTextSharp.text.pdf;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
 
namespace WebTest45
{
    public partial class Default : System.Web.UI.Page
    {
        string path
        {
            get { return ViewState["PATH"] == null ? string.Empty : (string)ViewState["PATH"]; }
            set { ViewState["PATH"] = value; }
        }
 
        string Arquivo
        {
            get { return ViewState["ARQUIVO"] == null ? string.Empty : (string)ViewState["ARQUIVO"]; }
            set { ViewState["ARQUIVO"] = value; }
        }
 
        protected void Page_Load(object sender, EventArgs e)
        {
            string msgErro = string.Empty;
            try
            {
                if (!IsPostBack)
                {
                    Session["PATH"] = "~//anexos//";
                }
            }
            catch (Exception ex)
            {
                msgErro = ex.Message;
                throw new Exception("Error-" + ex.Message);
            }
        }
 
 
        public void CriarDocumento(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(txtCriar.Text))
            {
                path = Session["PATH"].ToString();
                path = Server.MapPath(path);
 
                if (!Directory.Exists(path))
                {
                    Directory.CreateDirectory(path);
                }
 
                Arquivo = txtCriar.Text;
                hdfPath.Value = path + Arquivo;
                FileStream fs = new FileStream(path + Arquivo + ".pdf", FileMode.Create, FileAccess.Write, FileShare.None);
                Document doc = new Document();
                PdfWriter writer = PdfWriter.GetInstance(doc, fs);
                doc.Open();
                doc.Add(new Paragraph("TESTANDO ASSINATURA NOVA PARA O ARQUIVO:" + Arquivo));
                doc.Close();
            }
        }
 
        public void Download(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(Arquivo))
            {
                FileStream sourceFile = new FileStream(Server.MapPath("~//anexos//" + Arquivo + "Assinado.pdf"), FileMode.Open);
                float FileSize;
                FileSize = sourceFile.Length;
                byte[] fileContent = new byte[(int)FileSize];
                sourceFile.Read(fileContent, 0, (int)sourceFile.Length);
                sourceFile.Close();
                Response.ClearContent();
                Response.ClearHeaders();
                Response.Buffer = true;
                Response.ContentType = "application/octet-stream";
                Response.AddHeader("Content-Length", fileContent.Length.ToString());
                Response.AddHeader("Content-Disposition", "attachment; filename=Teste.pdf");
                Response.BinaryWrite(fileContent);
                Response.Flush();
                Response.End();
            }
        }
    }
}



using System;
using System.Runtime.InteropServices;
using System.Security.Cryptography.X509Certificates;
 
using Org.BouncyCastle.X509;
using Org.BouncyCastle.Security;
using iTextSharp.text.pdf;
using System.IO;
using iTextSharp.text.pdf.security;
using System.Collections.Generic;
using System.Collections;
 
namespace ANamespace
{
 
    [ClassInterface(ClassInterfaceType.AutoDual)]
    public class AClass
    {
        public void Assinador(string path_NomeArquivo)
        {
 
            X509CertificateParser cp = new X509CertificateParser();
 
            //SEARCH THE USER OF CERTIFICATES
            X509Certificate2 certClient = null;
            X509Store st = new X509Store(StoreName.My, StoreLocation.CurrentUser);
            st.Open(OpenFlags.MaxAllowed);
            X509Certificate2Collection collection = X509Certificate2UI.SelectFromCollection(st.Certificates, "Por Favor, Escolha um certificado:", "", X509SelectionFlag.SingleSelection);
            if (collection.Count > 0)
            {
                certClient = collection[0];
            }
            st.Close();
 
            //Get Cert Chain
            IList<Org.BouncyCastle.X509.X509Certificate> chain = new List<Org.BouncyCastle.X509.X509Certificate>();
            X509Chain x509chain = new X509Chain();
            x509chain.Build(certClient);
            foreach (X509ChainElement x509ChainElement in x509chain.ChainElements)
            {
                chain.Add(DotNetUtilities.FromX509Certificate(x509ChainElement.Certificate));
            }
 
            //FILE TO SIGN           
            PdfReader reader = new PdfReader(path_NomeArquivo + ".pdf");
 
            //NEW DOCUMENT
            FileStream resStream = new FileStream(path_NomeArquivo + "Assinado.pdf", FileMode.Create, FileAccess.ReadWrite);
 
            PdfStamper stamper = PdfStamper.CreateSignature(reader, resStream, '\0', null, true);
 
 
            PdfSignatureAppearance appearance = stamper.SignatureAppearance;
            appearance.ReasonCaption = "Descrição:";
            appearance.Reason = "Assinar Documento Teste";
            appearance.LocationCaption = "Local:";
            appearance.Location = "Defensoria Publica";
 
            //Place where is the Field of the digital signature
            if (stamper.Reader.Info["CreationDate"].ToString() == stamper.Reader.Info["ModDate"].ToString())
            {
                appearance.SetVisibleSignature(new iTextSharp.text.Rectangle(20, 10, 170, 60), 1, "Signed");
            }
 
 
            X509Certificate2Signature es = new X509Certificate2Signature(certClient, "SHA-1");
 
            //REQUESTS PASSWORD
            MakeSignature.SignDetached(appearance, es, chain, null, null, null, 0, CryptoStandard.CMS);
 
        }
 
    }
}
Posted
Comments
Member 11296812 10-Jan-20 10:02am    
Kindly share solution pls.

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