"The HTTP request was Forbidden with client Authentication Scheme 'Anonymous'"

Asked

Viewed 897 times

0

I am trying to communicate my application with the Cadsus web service from an Asp . net core application. I have using the code below, but whenever I try to recover the return of the query I get the error "The HTTP request was Forbidden with client Authentication Scheme 'Anonymous'".

   var address = new EndpointAddress("https://servicos.saude.gov.br/cadsus/v5r0/cadsusservice");
            BasicHttpBinding binding = new BasicHttpBinding(BasicHttpSecurityMode.Transport);
            binding.Security.Transport.ClientCredentialType = HttpClientCredentialType.Certificate;

            var cad = new CadsusServicePortTypeClient(CadsusServicePortTypeClient.EndpointConfiguration.CadsusServicePort, address);//binding, address
            cad.ClientCredentials.UserName.UserName = "-----------------------------";
            cad.ClientCredentials.UserName.Password = "---------------------"; 
            cad.ClientCredentials.ClientCertificate.SetCertificate(StoreLocation.LocalMachine,
                                                    StoreName.My,
                                                    X509FindType.FindBySubjectName, "");

            requestPesquisar itenspesquisa = new requestPesquisar();
            itenspesquisa.CNESUsuario = new CNESUsuarioType()
            {
                CNES = "*",
                Usuario = "*"
            };

            itenspesquisa.FiltroPesquisa = new FiltroPesquisa()
            {
                nomeCompleto = new NomeCompletoType { Nome = "*" },
                dataNascimentoSpecified = false,
                CPF = new CPFType { numeroCPF = "*" },
                tipoPesquisa = TipoPesquisaType.APROXIMADA
            };

            itenspesquisa.higienizar = false;
            ResultadoPesquisa[] resultWS = cad.pesquisarAsync(itenspesquisa).Result.responsePesquisar1;
            foreach (ResultadoPesquisa result in resultWS)
            {
                var teste = result.CNS;
            }

Any idea what it might be?

1 answer

0


A possible solution is to configure your application on the Web.Config or App.Config as follows:

If there is already a configuration for the protocol below change as following lines..

<protocolMapping>
        <add binding="wsHttpBinding" scheme="https" bindingConfiguration="WsHttpBindingConfig" />
    </protocolMapping> 

You can see more details of this solution in the link below: insert link description here

Browser other questions tagged

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