Login to the eCac system with C #

0

I'm doing some research, and I'm logging into the ecac site using my company's certificate ... using Chrome I can log into the system without any problem because chrome asks for the certificate to authenticate the login. I tried to implement a small test crawler, where I step my certificate in the request, but, the government website always returns error ... So I tried to use selenium with the chrome driver to login, but with that it is necessary that I select the certificate manually, and my idea is to do this automatically.

The source of the test I deployed to login is this:

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using System.Threading.Tasks;

namespace TestCrawler
{
    class Program
    {
        static void Main(string[] args)
        {
            string host = @"https://cav.receita.fazenda.gov.br/autenticacao/login";
            string certName = @"certificado.pfx";
            string password = @"senha";

            try
            {
                X509Certificate2 certificate = new X509Certificate2(certName, password);

                ServicePointManager.CheckCertificateRevocationList = false;
                ServicePointManager.ServerCertificateValidationCallback = (a, b, c, d) => true;
                ServicePointManager.Expect100Continue = true;
                HttpWebRequest req = (HttpWebRequest)WebRequest.Create(host);
                req.PreAuthenticate = true;
                req.AllowAutoRedirect = true;
                req.ClientCertificates.Add(certificate);
                req.Method = "POST";
                req.ContentType = "application/x-www-form-urlencoded";
                string postData = "login-form-type=cert";
                byte[] postBytes = Encoding.UTF8.GetBytes(postData);
                req.ContentLength = postBytes.Length;

                Stream postStream = req.GetRequestStream();
                postStream.Write(postBytes, 0, postBytes.Length);
                postStream.Flush();
                postStream.Close();
                WebResponse resp = req.GetResponse();

                Stream stream = resp.GetResponseStream();
                using (StreamReader reader = new StreamReader(stream))
                {
                    string line = reader.ReadLine();
                    while (line != null)
                    {
                        Console.WriteLine(line);
                        line = reader.ReadLine();
                    }
                }

                stream.Close();
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
            }
        }
    }
}

When analyzing the source of the ecac site, I have identified that in the login button by the certificate, a JS code is executed

  

onclick="javascript: document.loginCert.submit (); return false;

I tried to run this call, but I could not find the URL it runs ...

My other attempt was:

using System;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Security.Cryptography.X509Certificates;

namespace TestCrawler
{
    class Program
    {
        private static string host = @"https://cav.receita.fazenda.gov.br/";
        private static string certName = @"certificado.pfx";
        private static string password = @"senha";

        static async void Login()
        {
            X509Certificate2 certificate = new X509Certificate2(certName, password);

            var handler = new WebRequestHandler();
            handler.ClientCertificates.Add(certificate);

            using (var client = new HttpClient(handler))
            {
                client.BaseAddress = new Uri(host);
                client.DefaultRequestHeaders.Accept.Clear();
                client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("text/html"));

                // New code:
                using (HttpResponseMessage response = await client.GetAsync("autenticacao/Login/Certificado"))
                {
                    using (HttpContent content = response.Content)
                    {
                        string result = await content.ReadAsStringAsync();
                    }
                }
            }
        }

        static void Main(string[] args)
        {
            Login();
            Console.ReadLine();
        }
    }
}
    
asked by anonymous 19.02.2018 / 18:17

0 answers