ASP.NET - Firma digital en asp.net

 
Vista:
Imágen de perfil de JJ

Firma digital en asp.net

Publicado por JJ (3 intervenciones) el 10/06/2016 22:29:47
Buenas que tal, quisiera consultar si es posible la creación de una aplicación web que pueda firmar digitalmente un documento pdf, actualmente tengo una aplicación que firma de forma local, si embargo cuando publico la aplicación al iis me lanza una excepción de sesion (La sesion actual no es interactiva) quisiera saber como puedo acceder a los certificados del cliente y con ellos firmar un documento, de antemano gracias!
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder
sin imagen de perfil

Firma digital en asp.net

Publicado por Carolina (5 intervenciones) el 13/06/2016 21:31:02
Hola

Te adjunto un proyecto, que fue lo más parecido a lo que necesitas, espero te sea de utilidad.

Saludos
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar
Imágen de perfil de JJ

Firma digital en asp.net

Publicado por JJ (3 intervenciones) el 22/06/2016 23:43:05
Muchas gracias :)
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar
Imágen de perfil de JJ

Firma digital en asp.net

Publicado por JJ (3 intervenciones) el 22/06/2016 23:43:16
Muchas gracias :)
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar

Firma digital en asp.net

Publicado por Edgar Giovanni Andrade Santamaría (2 intervenciones) el 23/06/2016 08:12:28
hola que tal... yo tengo este pequeño codigo para firmas digitale en PDF

espero y te sirva

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()
{

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

}
}
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar

Firma digital en asp.net

Publicado por Javier Lopez Vargas (10 intervenciones) el 22/07/2016 23:49:01
Hola amigo lo puedes hacer de esta manera, espero te sirva.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
using System;
using System.Collections;
using System.IO;
using System.Security.Cryptography;
using System.Security.Cryptography.Pkcs;
using iTextSharp.text;
using iTextSharp.text.pdf;
using Org.BouncyCastle.X509;
using SysX509 = System.Security.Cryptography.X509Certificates;
 
/// <summary>
/// Helper para el firmado de PDFs con la librería iTextSharp
/// </summary>
 
public static class PDF
{
 
    /// Firma un documento
 
    public static void SignHashed(string Source, string Target, SysX509.X509Certificate2 Certificate, string Reason, string Location, bool AddVisibleSign)
    {
        X509CertificateParser objCP = new X509CertificateParser();
        X509Certificate[] objChain = new X509Certificate[] { objCP.ReadCertificate(Certificate.RawData) };
 
        PdfReader objReader = new PdfReader(Source);
        PdfStamper objStamper = PdfStamper.CreateSignature(objReader, new FileStream(Target, FileMode.Create), '\0');
        PdfSignatureAppearance objSA = objStamper.SignatureAppearance;
 
         if (AddVisibleSign)
            objSA.SetVisibleSignature(new Rectangle(100, 100, 300, 200), 1, null);
 
        objSA.SignDate = DateTime.Now;
        objSA.SetCrypto(null, objChain, null, null);
        objSA.Reason = Reason;
        objSA.Location = Location;
        objSA.Acro6Layers = true;
 
        objSA.Render = PdfSignatureAppearance.SignatureRender.NameAndDescription;
 
        PdfSignature objSignature = new PdfSignature(PdfName.ADOBE_PPKMS, PdfName.ADBE_PKCS7_SHA1);
 
        objSignature.Date = new PdfDate(objSA.SignDate);
 
        objSignature.Name = PdfPKCS7.GetSubjectFields(objChain[0]).GetField("CN");
 
        if (objSA.Reason != null)
 
            objSignature.Reason = objSA.Reason;
 
        if (objSA.Location != null)
 
            objSignature.Location = objSA.Location;
 
        objSA.CryptoDictionary = objSignature;
 
        int intCSize = 4000;
 
        Hashtable objTable = new Hashtable();
 
        objTable[PdfName.CONTENTS] = intCSize * 2 + 2;
 
        objSA.PreClose(objTable);
 
 
 
        HashAlgorithm objSHA1 = new SHA1CryptoServiceProvider();
 
 
        Stream objStream = objSA.RangeStream;
 
        int intRead = 0;
 
        byte[] bytBuffer = new byte[8192];
 
        while ((intRead = objStream.Read(bytBuffer, 0, 8192)) > 0)
 
            objSHA1.TransformBlock(bytBuffer, 0, intRead, bytBuffer, 0);
 
        objSHA1.TransformFinalBlock(bytBuffer, 0, 0);
 
        byte[] bytPK = SignMsg(objSHA1.Hash, Certificate, false);
 
        byte[] bytOut = new byte[intCSize];
 
 
 
        PdfDictionary objDict = new PdfDictionary();
 
        Array.Copy(bytPK, 0, bytOut, 0, bytPK.Length);
 
        objDict.Put(PdfName.CONTENTS, new PdfString(bytOut).SetHexWriting(true));
 
        objSA.Close(objDict);
    }
 
    /// Crea la firma CMS/PKCS #7
 
    private static byte[] SignMsg(byte[] Message, SysX509.X509Certificate2 SignerCertificate, bool Detached)
    {
        //Creamos el contenedor
        ContentInfo contentInfo = new ContentInfo(Message);
 
        //Instanciamos el objeto SignedCms con el contenedor
        SignedCms objSignedCms = new SignedCms(contentInfo, Detached);
 
 
        //Creamos el "firmante"
        CmsSigner objCmsSigner = new CmsSigner(SignerCertificate);
 
        // Include the following line if the top certificate in the
        // smartcard is not in the trusted list.
        objCmsSigner.IncludeOption = SysX509.X509IncludeOption.EndCertOnly;
 
        //  Sign the CMS/PKCS #7 message. The second argument is
        //  needed to ask for the pin.
        objSignedCms.ComputeSignature(objCmsSigner, false);
 
 
        //Encodeamos el mensaje CMS/PKCS #7
        return objSignedCms.Encode();
    }
}
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar