public XmlDocument refirmarDocumentos(XmlDocument XmlDocument) { string nombre = String.Empty; ManteUdoCertificadoDigital cerDigital = new ManteUdoCertificadoDigital(); //Se obtiene informacion del certificado digital string rutaCertificado = cerDigital.ObtenerRutaCertificado(); string clave = cerDigital.ObtenerPassCertificado(); DateTime fecha = DateTime.Now; string fechaFirma = fecha.ToString("yyyy'-'MM'-'dd'T'HH':'mm':'ssK"); string strRutReceptor, strFecha, strFechaFirma, strInfoCertificado, strFechaEmision, strCodSucursal; XmlDocument documento = new XmlDocument(); strRutReceptor = "DGICFE:RutReceptor"; strFecha = "DGICFE:Fecha"; strInfoCertificado = "DGICFE:X509Certificate"; strFechaFirma = "ns1:TmstFirma"; strFechaEmision = "ns1:FchEmis"; strCodSucursal = "ns1:CdgDGISucur"; nombre = String.Empty; documento = new XmlDocument(); documento.PreserveWhitespace = true; documento = XmlDocument; FirmaDigital firma = new FirmaDigital(); string infoCertificado = ProcTransformacion.ObtenerCadenaCertificado(); XmlNode nodoCertificado = documento.GetElementsByTagName(strInfoCertificado).Item(0); nodoCertificado.InnerXml = infoCertificado; // End Caratula / // Begin CFE / XmlNode nodoFechaFirma = documento.GetElementsByTagName(strFechaFirma).Item(0); nodoFechaFirma.InnerXml = fechaFirma; // End CFE / #region CFE_FIRMA // Elimino el nodo Firma del template XmlNode nodoFirma = documento.GetElementsByTagName("Signature").Item(0); documento.GetElementsByTagName("ns1:CFE").Item(0).RemoveChild(nodoFirma); //Crea nuevo documento con el CFE para firmar XmlDocument docFirma = new XmlDocument(); docFirma.PreserveWhitespace = true; XmlNode cfe = documento.DocumentElement.GetElementsByTagName("ns1:CFE").Item(0); XmlNode nuevo = docFirma.ImportNode(cfe, true); docFirma.AppendChild(nuevo); try { // Create a new CspParameters object to specify // a key container. CspParameters cspParams = new CspParameters(); cspParams.KeyContainerName = rutaCertificado; // Create a new RSA signing key and save it in the container. RSACryptoServiceProvider rsaKey = new RSACryptoServiceProvider(cspParams); // Obtengo cadena certificado / byte[] pfxBlob = File.ReadAllBytes(rutaCertificado); // Sign the XML document. firma.SignXml(docFirma, pfxBlob, rsaKey, clave); // Elimino el CFE del template // documento.DocumentElement.RemoveChild(cfe); // Agrego el CFE firmado // XmlNode cfeFirmado = docFirma.GetElementsByTagName("ns1:CFE").Item(0); nuevo = documento.ImportNode(cfeFirmado, true); documento.DocumentElement.AppendChild(nuevo); #endregion CFE_FIRMA } catch (Exception e) { } return(documento); }
/// <summary> /// Metodo para crear sobre para el comprobante /// </summary> /// <param name="cfe"></param> private Sobre CrearSobre(CFE cfe, bool sobreDgi) { Sobre sobre = new Sobre(cfe); string infoCertificado = ""; try { infoCertificado = ProcTransformacion.ObtenerCadenaCertificado(); if (infoCertificado.Equals("")) { sobre = null; } else { if (sobreDgi) { ManteUdoDocumento manteUdoDocumento = new ManteUdoDocumento(); string rutConfigurado = manteUdoDocumento.ObtenerRut(); //Proceso para DGI if (rutConfigurado != null) { sobre.RucReceptor = rutConfigurado;//214844360018;//219999830019 } else { sobre.RucReceptor = "214844360018";//219999830019 } sobre.X509Certificate = infoCertificado; sobre.ObtenerCertificadosCreados(listaCertificadosCreados); string xmlSobreDGI = ProcSerializacion.CrearXmlSobre(sobre); ProcTransformacion.GuardarSobrePrevio(sobre.NombrePrev, xmlSobreDGI, true); ProcTransformacion.TransformarSobre(sobre.NombrePrev, sobre.Nombre, sobre.ListaCertificados, "", true); } else { //Proceso para Tercero sobre.X509Certificate = infoCertificado; sobre.ObtenerCertificadosCreados(listaCertificadosCreados); string xmlSobreCliente = ProcSerializacion.CrearXmlSobre(sobre); ProcTransformacion.GuardarSobrePrevio(sobre.NombrePrev, xmlSobreCliente, false); if (!cfe.TextoLibreAdenda.Equals("")) { ProcTransformacion.TransformarSobre(sobre.NombrePrev, sobre.Nombre, sobre.ListaCertificados, cfe.TextoLibreAdenda, false); } else { ProcTransformacion.TransformarSobre(sobre.NombrePrev, sobre.Nombre, sobre.ListaCertificados, "", false); } } } } catch (Exception ex) { SAPbouiCOM.Framework.Application.SBO_Application.MessageBox("ERROR: " + ex.ToString()); } return(sobre); }