Query Status NFe C #

1

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

  

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

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

I'm using the code below, could anyone tell me 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 it asks me for the certificate and I can access WS. I've tried to use 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");
    
asked by anonymous 09.12.2014 / 16:57

3 answers

4

According to some research I found this link: link

Basically my app.config file was described as follows:

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

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

<binding name="NfeStatusServico2Soap12">
    <textMessageEncoding messageVersion="Soap12" />
    <httpsTransport authenticationScheme="Digest" requireClientCertificate="true" />
</binding>
    
09.12.2014 / 18:55
0

Cristiano, so I see you're trying to set up the settings for Endpoint and Binding in the arm, I've squeezed a bit to get it done because there are a lot of settings that are made from web.config . Try to use

Configuring 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;
            }
        }

Configuring 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));
    
11.12.2014 / 12:38
0

Dude the problem is authentication on the recipe server, they ask your application for certificate when trying to connect to them to consume. Example of service binding RecepcionEventSoap that stays in your App.config when after you add the web servisse (without a 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>
    
08.06.2015 / 22:26