public void FirmarUserPDFButton_Click(object sender, EventArgs e) { typeFormatSign typeFormatSign = typeFormatSign.PDF_PKCS7; Button btn = (Button)sender; switch (btn.CommandName) { case "PDF_PKCS7": typeFormatSign = typeFormatSign.PDF_PKCS7; break; case "PDF_PKCS7_T": typeFormatSign = typeFormatSign.PDF_PKCS7_T; break; case "PAdES_BASIC": typeFormatSign = typeFormatSign.PAdES_BASIC; break; case "PAdES_BES": typeFormatSign = typeFormatSign.PAdES_BES; break; case "PAdES_EPES": typeFormatSign = typeFormatSign.PAdES_EPES; break; } //Recuperamos la instancia del cliente ViafirmaClient clienteViafirma = ViafirmaClientFactory.GetInstance(); // Recuperamos el documento a firmar. Assembly assembly = Assembly.GetExecutingAssembly(); Stream fs = assembly.GetManifestResourceStream(Global.DEMO_FILE_PDF_PATH); byte[] datos_a_firmar = new byte[fs.Length]; fs.Read(datos_a_firmar, 0, datos_a_firmar.Length); // Enviamos a firmar el documento // En algunos casos, por ejemplo en el arranque de la aplicación puede ser interesante // Comprobar que efectivamente el servidor de firma está disponible try { System.Console.Write(clienteViafirma.ping("Prueba Conexión") + "\n"); } catch (System.Web.Services.Protocols.SoapException exc) { System.Console.WriteLine(exc.Message); } // Registramos el documento que deseamos firmar. Obteniendo un identificador temporal. // Este identificador temporal no es necesario que sea almacenado ya que sólo tiene validez durante el proceso de firma. string idTemporalFirma = null; try { //Creamos el objeto documento con los datos a firmar documento doc = new documento(); doc.nombre = "FicheroEjemplo.pdf"; doc.datos = datos_a_firmar; doc.typeFormatSign = typeFormatSign; doc.tipo = typeFile.PDF; // En algunos casos, por ejemplo en el arranque de la aplicación puede ser interesante // Comprobar que efectivamente el servidor de firma está disponible System.Console.Write(clienteViafirma.ping("Prueba Conexión") + "\n"); //Creamos la politica de firma policy pol = PolicyUtil.newPolicy(typeFormatSign, typeSign.ENVELOPED); // Registramos el documento que deseamos firmar. Obteniendo un identificador temporal. // Este identificador temporal no es necesario que sea almacenado ya que sólo tiene validez durante el proceso de firma. idTemporalFirma = clienteViafirma.PrepareSignWithPolicy(pol, doc); System.Console.Write("idTemporalFirma: " + idTemporalFirma); } catch (Exception exc) { System.Console.WriteLine(exc.Message); String messageError = exc.Message; Uri url = new Uri(HttpContext.Current.Request.Url, HttpContext.Current.Response.ApplyAppPathModifier("~/errorPage.aspx?errorMessage=" + exc.Message)); HttpContext.Current.Response.Redirect(url.AbsoluteUri); return; } System.Console.Write("idTemporalFirma: " + idTemporalFirma); //Logica del ejemplo para descagar el fichero con la extension correcta Session["extension"] = ".pdf"; // Iniciamos el proceso de firma redireccionando al usuario a Viafirma.. // Esto redireccionará al usuario a Viafirma para la firma del documento con el // identificador de firma indicado. clienteViafirma.Sign(idTemporalFirma); }
public void FirmarServerPDFButton_Click(object sender, EventArgs e) { //Recuperamos la instancia del cliente ViafirmaClient clienteViafirma = ViafirmaClientFactory.GetInstance(); // Recuperamos el documento a firmar. Assembly assembly = Assembly.GetExecutingAssembly(); Stream fs = assembly.GetManifestResourceStream(Global.DEMO_FILE_PDF_PATH); byte[] datos_a_firmar = new byte[fs.Length]; fs.Read(datos_a_firmar, 0, datos_a_firmar.Length); // En algunos casos, por ejemplo en el arranque de la aplicación puede ser interesante // Comprobar que efectivamente el servidor de firma está disponible System.Console.Write(clienteViafirma.ping("Prueba Conexión") + "\n"); //Creamos el objeto documento con los datos a firmar typeFormatSign tipoFirma = typeFormatSign.PAdES_BES; documento doc = new documento(); doc.nombre = "PDF_SELLOIMAGEN.pdf"; doc.datos = datos_a_firmar; doc.typeFormatSign = tipoFirma; doc.tipo = typeFile.PDF; //Obtengo la imagen a estampar Stream stampImage = assembly.GetManifestResourceStream(Global.DEMO_STAMPER_PATH); byte[] image = new byte[stampImage.Length]; stampImage.Read(image, 0, image.Length); String imageB64 = System.Convert.ToBase64String(image); //Creamos la politica de firma policy pol = PolicyUtil.newPolicy(tipoFirma, typeSign.DETACHED); PolicyUtil.AddParameter(pol, PolicyParams.DIGITAL_SIGN_PAGE, "1"); //Creamos el rectangle rectangle r = PolicyUtil.newRectangle(40, 100, 300, 240); PolicyUtil.AddParameter(pol, PolicyParams.DIGITAL_SIGN_RECTANGLE, PolicyUtil.rectangleToJson(r)); PolicyUtil.AddParameter(pol, PolicyParams.DIGITAL_SIGN_IMAGE_STAMPER, imageB64); PolicyUtil.AddParameter(pol, PolicyParams.DIGITAL_SIGN_STAMPER_TEXT, "Prueba Firmado por [CN]\ncon DNI [SERIALNUMBER]\ntrabajador de [O]\nen el departamento de [OU]"); PolicyUtil.AddParameter(pol, PolicyParams.DIGITAL_SIGN_STAMPER_HIDE_STATUS, "true"); idFirma = clienteViafirma.SignByServerWithPolicy(pol, doc, Global.ALIAS, Global.PASS_CERT); //Obtengo la infirmación del documento firmado byte[] signedDoc = clienteViafirma.getDocumentoCustodiado(idFirma); firmaInfoViafirma info = clienteViafirma.getSignInfo(idFirma); string signTimeStamp = info.signTimeStamp; //tsa_cert_notafter usuarioGenericoViafirmaEntry[] usersInfo = info.properties; foreach (usuarioGenericoViafirmaEntry userInfo in usersInfo) { if ("tsa_cert_notafter".Equals(userInfo.key)) { Console.WriteLine(userInfo.value); } } // Generamos la url a la página que gestiona la comunicación con Viafirma. // Uri url = new Uri(HttpContext.Current.Request.Url, HttpContext.Current.Response.ApplyAppPathModifier("~/ResultadoFirmaServidor.aspx")); // Log.Debug("Redireccionado al usuario a: " + url); // Guardamos el Id de Firma HttpContext.Current.Session["idFirma"] = idFirma; // Redirecciona a la url // HttpContext.Current.Response.Redirect(url.AbsoluteUri); }
public void FirmarUserXMLButton_Click(object sender, EventArgs e) { typeFormatSign typeFormatSign = typeFormatSign.XMLDSIG; Button btn = (Button)sender; switch (btn.CommandName) { case "XMLDSIG": typeFormatSign = typeFormatSign.XMLDSIG; break; case "XADES_BES": typeFormatSign = typeFormatSign.XADES_BES; break; case "XADES_EPES": typeFormatSign = typeFormatSign.XADES_EPES_ENVELOPED; break; case "XADES_T": typeFormatSign = typeFormatSign.XADES_T_ENVELOPED; break; case "XADES_XL": typeFormatSign = typeFormatSign.XADES_XL_ENVELOPED; break; case "XADES_A": typeFormatSign = typeFormatSign.XADES_A_ENVELOPED; break; } Assembly assembly = Assembly.GetExecutingAssembly(); Stream fs = assembly.GetManifestResourceStream(Global.DEMO_FILE_XML_PATH); byte[] datos_a_firmar = new byte[fs.Length]; fs.Read(datos_a_firmar, 0, datos_a_firmar.Length); try { ViafirmaClient clienteViafirma = ViafirmaClientFactory.GetInstance(); string idFirma; //Creamos el objeto documento con los datos a firmar documento doc = new documento(); doc.nombre = "prueba.xml"; doc.datos = datos_a_firmar; doc.typeFormatSign = typeFormatSign; doc.tipo = typeFile.XML; //Creamos la politica de firma policy pol = PolicyUtil.newPolicy(typeFormatSign, typeSign.ENVELOPED); // Registramos el documento que deseamos firmar. Obteniendo un identificador temporal. // Este identificador temporal no es necesario que sea almacenado ya que sólo tiene validez durante el proceso de firma. idFirma = clienteViafirma.PrepareSignWithPolicy(pol, doc); //Logica del ejemplo para descagar el fichero con la extension correcta Session["extension"] = ".xml"; clienteViafirma.Sign(idFirma); } catch (Exception ex) { Console.Write(ex.StackTrace); } }