Http Request, with Certificate X509

Asked

Viewed 484 times

-1

Good morning,

By accessing the following link :
"https://www.nfe.fazenda.gov.br/portal/downloadNFe.aspx?tipoConsulta=completa&tipoConteudo=XbSeqxE8pl8=&a=mOEyESJIJwMoSf9GhwkJRg7glhHoU0omjiV+4HmZZHwvpF4c9ivAFcAF04DQk1fb"

The site automatically calls the screen for digital certificate selection.

Once the certificate has been confirmed, it downloads a file, without having to click any later button...

I needed to do this in the background on my client’s machine that has this certificate..

Soon I am looking for a way to pass the certificate X509 (A1 or A3 with the password) for the request so that the site already goes straight to the file download, and I can get it in the programming via c#

I already tried with Webbrowser ( I did not identify a way to pass the certificate to the request in webbrowser)

I also tried via POST ( Which I believe is the wrong way, since it is not a webservice, but a site that automatically responds to the choice of certificate)

Seeking a light...

Note: I have tried with SELENIUM, but this way would have to have interaction on the screen, and everything has to happen in the background on the machine of the client that has the certificate.

Thank you.

1 answer

1


For this example I’ve put import your certified.pfx file to the root of your project.

Do not forget to set in the properties of the certificate.pfx (right click and select properties) in the option "Copy to output directory" leave as Copy Always

Propriedades certificado.pfx

public class SeuController : Controller
    {
        static readonly string senhaCertificado = "SuaSenha";

        public async Task<IActionResult> AcessarFazenda()
        {

            var handler = new HttpClientHandler();
            handler.ClientCertificateOptions = ClientCertificateOption.Manual;
            handler.SslProtocols = SslProtocols.Tls12;
            handler.ClientCertificates.Add(new X509Certificate2("certificado.pfx", senhaCertificado));

            using (HttpClient client = new HttpClient(handler))
            {

                HttpResponseMessage httpResponse = await client.GetAsync("https://www.nfe.fazenda.gov.br/portal/downloadNFe.aspx?tipoConsulta=completa&tipoConteudo=XbSeqxE8pl8=&a=mOEyESJIJwMoSf9GhwkJRg7glhHoU0omjiV+4HmZZHwvpF4c9ivAFcAF04DQk1fb");

                if (httpResponse.Content != null)
                {

                    //aqui você pode pegar o conteudo da requisição e salvar o arquivo ou devolver ele para a tela para download do usuário
                }
            }

            return View();
        }
    }

I don’t know if it contemplates everything you want but it makes the connection through the certificate.

I hope it helps

  • If you need any more details you can ask me. Your file will be available for download or will be saved in some directory ?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.