public async Task <string> ObtenerLoginTicketResponse(string argServicio, string argUrlWsaa, string argRutaCertX509Firmante, string argPassword)
        {
            this.RutaDelCertificadoFirmante = argRutaCertX509Firmante;

            string cmsFirmadoBase64    = null;
            string loginTicketResponse = null;
            //Task<loginCmsResponse> LoginTicketResponseAsync = null;
            loginCmsResponse LoginTicketResponseAsync = null;
            XmlNode          xmlNodoUniqueId          = null;
            XmlNode          xmlNodoGenerationTime    = null;
            XmlNode          xmlNodoExpirationTime    = null;
            XmlNode          xmlNodoService           = null;
            SecureString     strPasswordSecureString  = new SecureString();

            string ticket = null;

            foreach (char character in argPassword.ToCharArray())
            {
                strPasswordSecureString.AppendChar(character);
            }
            strPasswordSecureString.MakeReadOnly();

            // PASO 1: Genero el Login Ticket Request
            try
            {
                _globalUniqueID += 1;

                XmlLoginTicketRequest = new XmlDocument();
                XmlLoginTicketRequest.LoadXml(XmlStrLoginTicketRequestTemplate);

                xmlNodoUniqueId       = XmlLoginTicketRequest.SelectSingleNode("//uniqueId");
                xmlNodoGenerationTime = XmlLoginTicketRequest.SelectSingleNode("//generationTime");
                xmlNodoExpirationTime = XmlLoginTicketRequest.SelectSingleNode("//expirationTime");
                xmlNodoService        = XmlLoginTicketRequest.SelectSingleNode("//service");

                xmlNodoGenerationTime.InnerText = DateTime.Now.AddMinutes(-60).ToString("s");
                xmlNodoExpirationTime.InnerText = DateTime.Now.AddHours(+12).ToString("s");
                xmlNodoUniqueId.InnerText       = Convert.ToString(_globalUniqueID);
                xmlNodoService.InnerText        = argServicio;
                this.Service = argServicio;
            }
            catch (Exception excepcionAlGenerarLoginTicketRequest)
            {
                throw new Exception("***Error GENERANDO el LoginTicketRequest : " + excepcionAlGenerarLoginTicketRequest.Message + excepcionAlGenerarLoginTicketRequest.StackTrace);
            }

            X509Certificate2 certFirmante;

            try
            {
                certFirmante = CertificadosX509Lib.ObtieneCertificadoDesdeArchivo(RutaDelCertificadoFirmante, strPasswordSecureString);
                string[] subject = certFirmante.Subject.Split(',');
                foreach (string element in subject)
                {
                    if (element.Trim().StartsWith("SERIALNUMBER=CUIT "))
                    {
                        CUIT = ulong.Parse(element.Replace("SERIALNUMBER=CUIT ", ""));
                    }
                }
            }
            catch (Exception excepcionAlLeerCertificado)
            {
                throw new Exception("***Error Leyendo el Certificado : " + excepcionAlLeerCertificado.Message + excepcionAlLeerCertificado.StackTrace);
            }

            String      cacheKey = "N" + (argUrlWsaa + CUIT + argServicio).GetHashCode().ToString();
            XmlDocument cache    = new XmlDocument();

            if (File.Exists(CacheFilename(CUIT)))
            {
                cache.Load(CacheFilename(CUIT));
            }


            try
            {
                String   token = "";
                String   sign  = "";
                DateTime expirationDate;

                if (cache.SelectSingleNode("//root//" + cacheKey) != null)
                {
                    token = cache.SelectSingleNode("//root//" + cacheKey + "//" + "token").InnerText;
                    sign  = cache.SelectSingleNode("//root//" + cacheKey + "//" + "sign").InnerText;
                    try
                    {
                        expirationDate = DateTime.Parse(cache.SelectSingleNode("//root//" + cacheKey + "//" + "expirationDate").InnerText);
                        if (!token.Equals("") && (DateTime.Now < expirationDate))
                        {
                            this.ExpirationTime = expirationDate;
                            this.Token          = token;
                            this.Sign           = sign;
                            return("");
                        }
                    }
                    catch
                    {
                    }
                }

                // Convierto el login ticket request a bytes, para firmar
                Encoding EncodedMsg = Encoding.UTF8;
                byte[]   msgBytes   = EncodedMsg.GetBytes(XmlLoginTicketRequest.OuterXml);

                // Firmo el msg y paso a Base64
                byte[] encodedSignedCms = CertificadosX509Lib.FirmaBytesMensaje(msgBytes, certFirmante);
                cmsFirmadoBase64 = Convert.ToBase64String(encodedSignedCms);
            }
            catch (Exception excepcionAlFirmar)
            {
                throw new Exception("***Error FIRMANDO el LoginTicketRequest : " + excepcionAlFirmar.Message);
            }

            // PASO 3: Invoco al WSAA para obtener el Login Ticket Response
            try
            {
                var binding = new BasicHttpBinding();
                binding.Security.Mode = BasicHttpSecurityMode.Transport;

                Wsaa.LoginCMSClient servicioWsaa = new Wsaa.LoginCMSClient(binding, new EndpointAddress(argUrlWsaa));
                LoginTicketResponseAsync = await servicioWsaa.loginCmsAsync(cmsFirmadoBase64);

                ticket = LoginTicketResponseAsync.loginCmsReturn;
            }
            catch (Exception excepcionAlInvocarWsaa)
            {
                throw new Exception("***Error INVOCANDO al servicio WSAA : " + excepcionAlInvocarWsaa.Message);
            }


            // PASO 4: Analizo el Login Ticket Response recibido del WSAA
            try
            {
                XmlLoginTicketResponse = new XmlDocument();
                XmlLoginTicketResponse.LoadXml(ticket);

                this.UniqueId       = UInt32.Parse(XmlLoginTicketResponse.SelectSingleNode("//uniqueId").InnerText);
                this.GenerationTime = DateTime.Parse(XmlLoginTicketResponse.SelectSingleNode("//generationTime").InnerText);
                this.ExpirationTime = DateTime.Parse(XmlLoginTicketResponse.SelectSingleNode("//expirationTime").InnerText);
                this.Sign           = XmlLoginTicketResponse.SelectSingleNode("//sign").InnerText;
                this.Token          = XmlLoginTicketResponse.SelectSingleNode("//token").InnerText;

                if (cache.SelectSingleNode("//root") == null)
                {
                    cache.RemoveAll();
                    cache.AppendChild(cache.CreateElement("root"));
                }
                XmlNode rootNode = cache.SelectSingleNode("//root");
                XmlNode keyNode  = rootNode.SelectSingleNode(cacheKey);
                if (keyNode == null)
                {
                    keyNode = rootNode.AppendChild(cache.CreateElement(cacheKey));
                }
                if (keyNode.SelectSingleNode("token") == null)
                {
                    keyNode.AppendChild(cache.CreateElement("token"));
                }
                if (keyNode.SelectSingleNode("sign") == null)
                {
                    keyNode.AppendChild(cache.CreateElement("sign"));
                }
                if (keyNode.SelectSingleNode("expirationDate") == null)
                {
                    keyNode.AppendChild(cache.CreateElement("expirationDate"));
                }
                keyNode.SelectSingleNode("token").InnerText          = this.Token;
                keyNode.SelectSingleNode("sign").InnerText           = this.Sign;
                keyNode.SelectSingleNode("expirationDate").InnerText = this.ExpirationTime.ToString();
                cache.Save(CacheFilename(CUIT));
            }
            catch (Exception excepcionAlAnalizarLoginTicketResponse)
            {
                throw new Exception("***Error ANALIZANDO el LoginTicketResponse : " + excepcionAlAnalizarLoginTicketResponse.Message);
            }

            return(loginTicketResponse);
        }
Ejemplo n.º 2
0
 public LoginCMSClient(EndpointConfiguration endpointConfiguration, string remoteAddress) :
     base(LoginCMSClient.GetBindingForEndpoint(endpointConfiguration), new System.ServiceModel.EndpointAddress(remoteAddress))
 {
     this.Endpoint.Name = endpointConfiguration.ToString();
     ConfigureEndpoint(this.Endpoint, this.ClientCredentials);
 }
Ejemplo n.º 3
0
 public LoginCMSClient(EndpointConfiguration endpointConfiguration) :
     base(LoginCMSClient.GetBindingForEndpoint(endpointConfiguration), LoginCMSClient.GetEndpointAddress(endpointConfiguration))
 {
     this.Endpoint.Name = endpointConfiguration.ToString();
     ConfigureEndpoint(this.Endpoint, this.ClientCredentials);
 }
Ejemplo n.º 4
0
 public LoginCMSClient() :
     base(LoginCMSClient.GetDefaultBinding(), LoginCMSClient.GetDefaultEndpointAddress())
 {
     this.Endpoint.Name = EndpointConfiguration.LoginCms.ToString();
     ConfigureEndpoint(this.Endpoint, this.ClientCredentials);
 }
Ejemplo n.º 5
0
 private static System.ServiceModel.EndpointAddress GetDefaultEndpointAddress()
 {
     return(LoginCMSClient.GetEndpointAddress(EndpointConfiguration.LoginCms));
 }
Ejemplo n.º 6
0
 private static System.ServiceModel.Channels.Binding GetDefaultBinding()
 {
     return(LoginCMSClient.GetBindingForEndpoint(EndpointConfiguration.LoginCms));
 }