Jyina
Jyina

Reputation: 2902

How to configure WCF client binding when using X509 certificate?

I am trying to set the credentials in my WCF client as shown below.

            ClientCredentials loginCredentials = new ClientCredentials();
            loginCredentials.UserName.UserName = this.UserId;
            loginCredentials.UserName.Password = this.Password;
            loginCredentials.ClientCertificate.Certificate = new X509Certificate2(this.Certificate);

            var defaultCredentials = channelFactory.Endpoint.Behaviors.Find<ClientCredentials>();
            channelFactory.Endpoint.Behaviors.Remove(defaultCredentials);
            channelFactory.Endpoint.Behaviors.Add(loginCredentials);

I have the wcf client binding setup as below.

    <system.serviceModel>
    <bindings>
        <basicHttpBinding>
            <binding name="PPWSApiOrderBinding" closeTimeout="00:01:00"
                openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00"
                allowCookies="false" bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard"
                maxBufferSize="65536" maxBufferPoolSize="524288" maxReceivedMessageSize="65536"
                messageEncoding="Text" textEncoding="utf-8" transferMode="Buffered"
                useDefaultWebProxy="true">
                <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384"
                    maxBytesPerRead="4096" maxNameTableCharCount="16384" />
                <security mode="Transport">
                    <transport clientCredentialType="None" proxyCredentialType="None"
                        realm="" />
                    <message clientCredentialType="UserName" algorithmSuite="Default" />
                </security>
            </binding>
        </basicHttpBinding>
    </bindings>
    <client>
        <endpoint address="https://ws.test.globalgateway.com:443/wsapi/services"
            binding="basicHttpBinding" bindingConfiguration="PPWSApiOrderBinding"
            contract="PPWebService.PPWSApiOrder" name="PPWSApiOrderPort" />
    </client>
</system.serviceModel>

I am receiving an error "The http request is unauthorized with client authentication scheme 'anonymous'. The authentication header received from the server was 'Basic realm=..." The remote server returned an error: (401) Unauthorized.

Any help is appreciated. I am not sure what needs to be changed in the binding section of my app.config file. Thanks!

Upvotes: 0

Views: 1840

Answers (1)

MethodMan
MethodMan

Reputation: 18863

looks like your error could be in the Transport node

look at this MSDN page for the reference

here is an example.

<wsHttpBinding>
<binding name="TransportSecurity">
    <security mode="Transport" />
       <transport clientCredentialType = "Windows" />
    </security>
</binding>
</wsHttpBinding >

or

<wsHttpBinding>
<binding name="MessageSecurity">
    <security mode="Message" />
       <message clientCredentialType = "Certificate" />
    </security>
</binding>
</wsHttpBinding >

HttpBinding MSDN

Upvotes: 1

Related Questions