Query Status Nfe C#

Asked

Viewed 7,352 times

2

I am trying to perform the service status query for Nfe 3.10 but always is returning the following message:

The HTTP request is prohibited with the authentication scheme client 'Anonymous'.

I’m using Visual Studio 2012 and . NET Framework 4.5.

I’m using the code below, would someone let me know if something is missing?

String caminho = "C:\\Arquivos\\Upload\\certificado.pfx";
String senha = "123456789";
X509Certificate2 certificado = new X509Certificate2(caminho, senha);

TConsStatServ statusServer = new TConsStatServ();
statusServer.versao = "3.10";
statusServer.tpAmb = TAmb.Item2;
statusServer.cUF = TCodUfIBGE.Item42;
statusServer.xServ = TConsStatServXServ.STATUS;

XmlDocument doc = ObjectToXMLDocument(statusServer);

nfeCabecMsg nfeCabecalho = new nfeCabecMsg();
nfeCabecalho.cUF = "42";
nfeCabecalho.versaoDados = "3.10";

NfeStatusServico2Soap12Client statusServicoClient = new NfeStatusServico2Soap12Client();
System.ServiceModel.EndpointAddress endereco = new System.ServiceModel.EndpointAddress("https://homologacao.nfe.sefaz.rs.gov.br/ws/NfeStatusServico/NfeStatusServico2.asmx");
statusServicoClient.Endpoint.Address = endereco;
statusServicoClient.ClientCredentials.ClientCertificate.Certificate = certificado;

var retorno = statusServicoClient.nfeStatusServicoNF2(ref nfeCabecalho, doc);

Through the browser he asks me for the certificate and I can access the WS. I’ve tried using the calls:

X509Certificate2 certificado = new X509Certificate2(caminho, senha, X509KeyStorageFlags.MachineKeySet);
System.ServiceModel.EndpointAddress endereco = new System.ServiceModel.EndpointAddress("https://homologacao.nfe.sefaz.rs.gov.br/ws/NfeStatusServico/NfeStatusServico2.asmx?WSDL");
  • Are you encountering difficulties only in version "3.10" or "2.00" as well? Also, the CNPJ of the authorized certificate is the same certificate you are using to call the query?

3 answers

6


As some researches found this link: http://balaiotecnologico.blogspot.com.br/2011/03/problemas-comuns-ao-consumir-web.html

Basically in my file app.config was described as follows:

<binding name="NfeStatusServico2Soap12">
    <textMessageEncoding messageVersion="Soap12" />
    <httpsTransport />
</binding>

Just add the information described in link and solved the problem:

<binding name="NfeStatusServico2Soap12">
    <textMessageEncoding messageVersion="Soap12" />
    <httpsTransport authenticationScheme="Digest" requireClientCertificate="true" />
</binding>

1

Face the problem is authentication on the recipe server, they ask certified your application when trying to connect to them to consume. Example of Binding service Recepcaoeventosoap which sits on your App.config when after you add the web it served (without certificate on your machine it is not possible to add the web service):

   <bindings>
       <basicHttpBinding>
           <binding name="RecepcaoEventoSoap">
               <security mode="Transport">
                   <transport clientCredentialType="Certificate" proxyCredentialType="None" realm=""/>
                   <message clientCredentialType="Certificate" algorithmSuite="Default"/>
               </security>
           </binding>
           <binding name="RecepcaoEventoSoap1"/>
       </basicHttpBinding>
   </bindings>

1

Cristiano, from what I see you are trying to mount on the arm the settings of Endpoint and of Binding, I’ve worked a little bit to be able to do that, because it has a lot of settings that are made of web.config. Try to use

Configuration of Bind

public CustomBinding Binding
        {
            get
            {
                TextMessageEncodingBindingElement securityElement = new TextMessageEncodingBindingElement();
                securityElement.MaxReadPoolSize = 64;
                securityElement.MaxWritePoolSize = 16;
                securityElement.MessageVersion = MessageVersion.Soap12;
                securityElement.WriteEncoding = Encoding.UTF8;
                securityElement.ReaderQuotas.MaxDepth = 32;
                securityElement.ReaderQuotas.MaxStringContentLength = 8192;
                securityElement.ReaderQuotas.MaxArrayLength = 16384;
                securityElement.ReaderQuotas.MaxBytesPerRead = 4096;
                securityElement.ReaderQuotas.MaxNameTableCharCount = 16384;
                HttpsTransportBindingElement httpsTransport = new HttpsTransportBindingElement();
                httpsTransport.ManualAddressing = false;
                httpsTransport.MaxBufferPoolSize = 524288;
                httpsTransport.MaxReceivedMessageSize = 65536;
                httpsTransport.AllowCookies = false;
                httpsTransport.AuthenticationScheme = System.Net.AuthenticationSchemes.Digest;
                httpsTransport.BypassProxyOnLocal = false;
                httpsTransport.DecompressionEnabled = true;
                httpsTransport.HostNameComparisonMode = HostNameComparisonMode.StrongWildcard;
                httpsTransport.KeepAliveEnabled = true;
                httpsTransport.MaxBufferSize = 65536;
                httpsTransport.ProxyAuthenticationScheme = System.Net.AuthenticationSchemes.Anonymous;
                httpsTransport.Realm = "";
                httpsTransport.TransferMode = TransferMode.Buffered;
                httpsTransport.UnsafeConnectionNtlmAuthentication = false;
                httpsTransport.UseDefaultWebProxy = true;
                httpsTransport.RequireClientCertificate = true;
                CustomBinding binding = new CustomBinding(securityElement, httpsTransport);
                return binding;
            }
        }

Configuration of Endpoint

private EndpointAddress RemoteAddress(NotaFiscalEletronicaEntity nfe)
    {
        string uri = "";
        if (nfe.TpAmb == TpAmbEnum.Homologacao)
            uri = "https://homologacao.nfe.sefaz.rs.gov.br/ws/NfeRetAutorizacao/NFeRetAutorizacao.asmx";
        else uri = "https://nfe.sefaz.rs.gov.br/ws/NfeRetAutorizacao/NFeRetAutorizacao.asmx";

        EndpointAddress remoteAddress = new EndpointAddress(uri);

        return remoteAddress;
    }

Using

NfeRetAutorizacaoSoap12Client nfeRetAutorizacao = new NfeRetAutorizacaoSoap12Client(this.Binding, this.RemoteAddress(nfe));

Browser other questions tagged

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