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.

2
  • Hi did you resolve this? Facing something similar. Thanks
    – tjeuten
    Nov 4, 2021 at 22:54
  • @tjeuten I think so, but I don't really remember none of it anymore :( Sorry. If I do, I ping you here
    – andre_ss6
    Nov 5, 2021 at 5:58

0

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Browse other questions tagged or ask your own question.