Problem communicating with eSocial webservice

1

I have the following problem communicating with eSocial webservice, I'm using C # so I added the webservice reference in my project and now I need to send an XML, and what I thought was: to establish a secure connection, define a certificate, and open the connection, and then send the XML. I did this in the following way:

// Crio a variavel de envio de lote
ServicoEnviarLoteEventosClient enviarLote = new ServicoEnviarLoteEventosClient();

I have a small snippet of code that looks for the certificate in question on my local computer and arrows it in this variable as follows:

enviarLote.ClientCredentials.ClientCertificate.SetCertificate(
                 x509.SubjectName.Name, store.Location, StoreName.My);

I open the connection:

enviarLote.Open();

Then I try to upload:

var resposta = enviarLote.EnviarLoteEventos(System.Xml.Linq.XElement.Load(caminhoXML)); 

But when trying to return an error: Could not establish trust relationship for the SSL/TLS secure channel with authority

I have also installed the certificate chain provided by eSocial ....

Can anyone help me? If anyone has doubts about XML signature I can help ...

    
asked by anonymous 20.11.2017 / 18:06

2 answers

1

Thanks for referencing my example page, Leo!

I also created some time ago a page with tips on how to access the eSocial service, including some of the ones you posted in your question and answer. I'll take advantage of the subject of your question to put these tips here, to help anyone with similar problems.

To access the eSocial submission service, in the Restricted Production environment, the URL should be this:

  

link

In addition, according to the eSocial Developer's Guide v1.6.3, page 83, item '7.9. You should also install on the machine that will access the Chain of Certificates service issued on 06/02/2017 by Serpro, which are 3 certificates that can be obtained at this address:

>
  

link

According to the 02.03 item of the eSocial Portal FAQ page, certificates must be installed in the order that they are arranged on this Serpro page, and:
The Certification Authority Brazilian root v5 must be installed in the root CA repository. The SERPRO Certification Authority v4 and SERPRO Certification Authority Final v5 must be installed in the intermediate CA repository.

Remember that it is also necessary to have a valid digital certificate (A1 or A3, e-CNPJ or e-CPF) installed on the computer that will access the web service, which must be used to access the service. One tip: When I started the tests with eSocial, I was almost a week banging my head to get the first access, when I finally discovered that in my case (e-CNPJ A1), it was necessary to select the Mark this key how to exportable and store ) and install my certificate in the store Personal / em>), Current User and Current User

Regarding the code used to access the service, the binding service (I used BasicHttpBinding or BasicHttpsBinding ) should be configured to use SecurityMode = Transport (for HTTPS) and ClientCredentialType = Certificate (to specify a certificate), something like this:

 var urlServicoEnvio = @"https://webservices.producaorestrita.esocial.gov.br/servicos/empregador/enviarloteeventos/WsEnviarLoteEventos.svc";
 var address = new EndpointAddress(urlServicoEnvio);
 var binding = new BasicHttpsBinding();  //Disponível desde .NET Framework 4.5
 // ou:
 //var binding = new BasicHttpBinding(BasicHttpsSecurityMode.Transport);
 binding.Security.Transport.ClientCredentialType = HttpClientCredentialType.Certificate;

 var wsClient = new WsEnviar.ServicoEnviarLoteEventosClient(binding, address);
 wsClient.ClientCredentials.ClientCertificate.Certificate = x509Cert;

 var retornoEnvioXElement = wsClient.EnviarLoteEventos(loteEventosXDoc.Root);
 wsClient.Close();

As for the WsEnviar.ServicoEnviarLoteEventosClient class used in code, it was created by Visual Studio when adding a Service Reference , using the same URL as the service, but adding the singleWsdl in>:

  

link

In VS it is also possible to add a reference to the service by directly using the WsSubmitEvents-v1_1_0.wsdl file available in the eSocial Communication Package (latest version 1.4.1) , which can be found on the technical documentation page of the eSocial Portal.

This tool, Add Service Reference , will create a client class to consume the web service, in the case of the WsEnviar.ServicoEnviarLoteEventosClient sample, which inherits the System.ServiceModel.ClientBase class .

You can also use the svcutil.exe command line tool, which will likewise create a client class inheriting the System.ServiceModel.ClientBase class. Alternatively, you can also use the older wsdl.exe command line tool for ASMX-based .NET Framework 2 time services that will also create a client class, but this time inheriting the System.Web.Services.Protocols.SoapHttpClientProtocol class. But in that case the code to consume the service would be a bit different.

Following all these steps, access to the eSocial web service should work.

    
20.02.2018 / 04:31
0

To resolve the secure connection problem, you need to install the government certificate string at:

Autoridades de Certificação Raiz Confiáveis

Remember, for each certificate be sure to put in this folder as I had forgotten.

To Download the Certificate Chain go to:

  

link

The code structure I have given above is correct, the submission will be done if the XML is structured correctly, for examples of valid XML:

  

link

I hope I have collaborated with someone!

    
21.11.2017 / 13:20