Skip to main content
2 de 2
se añadieron 9250 caracteres en el cuerpo

esto lo probé en una aplicación de consola y funciono, pruébalo con tus datos y luego lo llevas a tu aplicación final y limpias lo que te sobre, espero poder ayudar a alguien y que no pierda el tiempo como me paso a mi

using System;
using System.Configuration;
using System.IO;
using System.Security.Cryptography;
using System.Text;
using System.Xml;

namespace SoapServices
{

    class program
    {
        public static void Main()
        {
            PasswordDigestRequest ale = new PasswordDigestRequest();
            ale.CreateHeader();
            Console.WriteLine(ale.CreateHeader());

        }

    }

    public class PasswordDigestRequest
    {
        internal string Username { get; set; }
        internal string Password { get; set; }
        internal string Created { get; set; }

        public PasswordDigestRequest()
        {
            Username = "tuusario";
            Password = "tupassword";
            Created = DateTime.Now.ToString("yyyy-MM-ddTHH:mm:ss.fffZ");
        }

        internal static string CreateHashedPassword(byte[] nonce, string created, string password)
        {
            byte[] createdBytes = Encoding.UTF8.GetBytes(created);
            byte[] passwordBytes = Encoding.UTF8.GetBytes(password);
            byte[] combined = new byte[createdBytes.Length + nonce.Length + passwordBytes.Length];
            Buffer.BlockCopy(nonce, 0, combined, 0, nonce.Length);
            Buffer.BlockCopy(createdBytes, 0, combined, nonce.Length, createdBytes.Length);
            Buffer.BlockCopy(passwordBytes, 0, combined, nonce.Length + createdBytes.Length, passwordBytes.Length);

            System.Net.HttpWebRequest myReq = (System.Net.HttpWebRequest)System.Net.WebRequest.Create("");
            
               

            return Convert.ToBase64String(SHA1.Create().ComputeHash(combined));
        }

        internal static byte[] GetNonce()
        {
            byte[] nonce = new byte[0x10];
            RandomNumberGenerator generator = new RNGCryptoServiceProvider();
            generator.GetBytes(nonce);
            return nonce;
        }

        
        private System.Data.DataSet xmlResponse(string NumeroReferencia)
        {
            System.Data.DataSet ds = new System.Data.DataSet();
            try
            {
                
                 string xml;
                byte[] nonce = GetNonce();
                string nonceStr = Convert.ToBase64String(nonce);
                string hashedPassword = CreateHashedPassword(nonce, Created, Password);

                var soapStr = new StringBuilder();

                soapStr.Append("");
                soapStr.Append("");
                soapStr.Append("");
                soapStr.Append("");
                soapStr.Append(Username);
                soapStr.Append("");
                soapStr.Append("");
                soapStr.Append(hashedPassword);
                soapStr.Append("");
                soapStr.Append("");
                soapStr.Append(nonceStr);
                soapStr.Append("");
                soapStr.Append("");
                soapStr.Append(Created);
                soapStr.Append("");
                soapStr.Append("");
                soapStr.Append("");
                soapStr.Append("");
                string xml;
                
                
                xml = "<soap:Envelope xmlns:mpi=\"http://osb.bancochile.cl/bch/neg/tarjetaCredito/pagoAvanceTarjetaCredito/pagarTarjetaCreditoRq/mpi\" xmlns:ns=\"http://osb.bancochile.cl/ent/bch/neg/mci/cabeceraServicios/v/4\" xmlns:ns1=\"http://osb.bancochile.cl/ent/bch/infra/mci/cabeceraServicios/v/3\" xmlns:soap=\"http://www.w3.org/2003/05/soap-envelope\">" +
                                      "<soap:Header><wsse:Security soap:mustUnderstand=\"true\" xmlns:wsse=\"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd\" xmlns:wsu=\"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd\">" +
                "<wsse:UsernameToken wsu:Id=\"UsernameToken-42A5B67414DFBD0FF3169523712151218\">" +
                "<wsse:Username>sistema-murex</wsse:Username>"+
                "<wsse:Password Type=\"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordDigest\">{password}</wsse:Password>" +
                "<wsse:Nonce EncodingType=\"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-soap-message-security-1.0#Base64Binary\">{nonce}</wsse:Nonce>" +
                "<wsu:Created>{created}</wsu:Created>"+
                "</wsse:UsernameToken>"+
            "</wsse:Security>"+
      "<ns:headerRequest>"+
         "<ns:datosConsumidor>"+
            "<ns1:usuario>15125990-1</ns1:usuario>"+
         "</ns:datosConsumidor>"+
         "<ns:datosTransaccion>"+
            "<ns:idTransaccionNegocio>@TRANSACCION@</ns:idTransaccionNegocio>"+
            "<!--Optional: MDW_1908_20230920-1351_161689  -->"+
         "</ns:datosTransaccion>"+
      "</ns:headerRequest>"+
   "</soap:Header>"+
   "<soap:Body>"+
      "<mpi:pagarTarjetaCreditoRq>"+
         "<mpi:fechaHoraCorrienteTransaccion>2023-09-20T10:16:10.000</mpi:fechaHoraCorrienteTransaccion>"+
         "<mpi:horario>PM</mpi:horario>"+
           "<mpi:datosCargo>"+
            "<mpi:cuentaCargo>1621742608</mpi:cuentaCargo>"+
            "<!--Optional:-->"+
            "<mpi:rutClienteCargo>8782091-2</mpi:rutClienteCargo>"+
            "<!--Optional:-->"+
            "<mpi:nombreClienteCargo>Miguel Perez</mpi:nombreClienteCargo>"+
            "<!--Optional:-->"+
            "<mpi:bancaCargo>Personas</mpi:bancaCargo>"+
            "<!--Optional:-->"+
            "<mpi:segmentoCargo>Generico Personas</mpi:segmentoCargo>"+
         "</mpi:datosCargo>"+
         "<!--Optional:-->"+
         "<mpi:listaDataCartola>"+
            "<!--Zero or more 2252590600 2661901 repetitions:-->"+
            "<mpi:datosCartola>"+
               "<!--Optional:-->"+               
               "<!--Optional:-->"+              
            "</mpi:datosCartola>"+
         "</mpi:listaDataCartola>"+
      "</mpi:pagarTarjetaCreditoRq>"+
   "</soap:Body>"+
"</soap:Envelope>";
                
                //esto lo mejoras dentro del xml, yo lo hice asì para probar 
                xml = xml.Replace("{password}", hashedPassword);
                xml = xml.Replace("{nonce}", nonceStr);
                xml = xml.Replace("{created}", Created);
                xml = xml.Replace("@TRANSACCION@", String.Concat("IPPTARJ", DateTime.Now.ToString("ddMMyyyyhmmssms")));
                xml = xml.Replace("@EMISOR@", String.Concat("iDEm", DateTime.Now.ToString("ddMMyyyyhmmssms")));


                string urlconfig = "tuendpoint";

                string url = urlconfig;

                System.Net.HttpWebRequest myReq = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(url);
            ASCIIEncoding encoding = new ASCIIEncoding();
            byte[] buffer = encoding.GetBytes(xml);
            string response;
            myReq.AllowWriteStreamBuffering = false;
            myReq.Method = "POST";
            myReq.Accept = "gzip,deflate";
            myReq.ContentType = @"application / soap + xml; charset = UTF - 8; action = \""pagarTarjetaCredito\""";
            myReq.Headers["Authorization"] = "Basic c2lGdGVtYS1tdXJlEDTCQG5rMC4wCQ=="; //ESTE CODIGO TE LO DEBE DAR TU PROVEEDOR DE SERVICIOS 
            myReq.ContentLength = buffer.Length;
            myReq.Headers.Add("SOAPAction", "");
            System.Net.ServicePointManager.ServerCertificateValidationCallback = ((sender1, certificate, chain, sslPolicyErrors) => true);


                using (Stream post = myReq.GetRequestStream())
                {
                    post.Write(buffer, 0, buffer.Length);
                }

                System.Net.HttpWebResponse myResponse = (System.Net.HttpWebResponse)myReq.GetResponse();

                Stream responsedata = myResponse.GetResponseStream();
                StreamReader responsereader = new StreamReader(responsedata);
                response = responsereader.ReadToEnd();

                ds.ReadXml(new XmlTextReader(new StringReader(response)));
                myResponse.Close();
                responsereader.Close();
                responsedata.Close();
                return ds;
            }
            catch (Exception ex)
            {
                return null;
            }
            finally
            {
                ds.Dispose();
            }
        }
    }
}