5

I'm trying to connect to a SOAP service using the WCF Client generator tool in Visual Studio 2017. However, when I make a request, I get this error:

"The http request is unauthorized with client authentication scheme 'Anonymous'".

I'm trying to use certificate authentication. This is my App.config:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <startup> 
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.1" />
    </startup>
    <system.serviceModel>
        <diagnostics performanceCounters="Default" />
        <behaviors>
            <endpointBehaviors>
                <behavior name="NewBehavior0">
                    <clientCredentials useIdentityConfiguration="true">
                        <clientCertificate findValue="portalservicos.jucemg.mg.gov.br"
                            storeLocation="LocalMachine" storeName="TrustedPeople" x509FindType="FindByIssuerName" />
                        <serviceCertificate>
                            <authentication certificateValidationMode="PeerOrChainTrust"
                                trustedStoreLocation="LocalMachine" />
                        </serviceCertificate>
                    </clientCredentials>
                </behavior>
            </endpointBehaviors>
        </behaviors>
        <bindings>
            <basicHttpBinding>
                <binding name="NewBinding2">
                    <security mode="Transport">
                        <transport clientCredentialType="Certificate" />
                    </security>
                </binding>
            </basicHttpBinding>
        </bindings>
        <client>
            <endpoint address="https://newsrm.jucemg.mg.gov.br:443/empreendimento/service/ViabilidadePrefeituraWS"
                behaviorConfiguration="NewBehavior0" binding="basicHttpBinding"
                bindingConfiguration="NewBinding2" contract="ViabilidadePrefeituraWS.ViabilidadePrefeituraWS"
                name="ViabilidadePrefeituraWSPort">
            </endpoint>
        </client>
    </system.serviceModel>
</configuration>

This is my code:

class Program
{
    static void Main(string[] args)
    {
        var ws = new ViabilidadePrefeituraWSClient();
        ws.callService("");
    }
}

When I set a breakpoint there, I see that the certificate is indeed being loaded (ws.ClientCredentials.ClientCertificate shows the correct certificate). Somehow though, it seems that the certificate is not being sent, or the configuration is somehow not being used, since the error mentions Authentication Scheme "Anonymous".

I saw this question (WCFTestClient The HTTP request is unauthorized with client authentication scheme 'Anonymous') and other similar questions, however none of them were about using certificates to authenticate.

andre_ss6
  • 1,195
  • 1
  • 13
  • 34

0 Answers0