简体   繁体   中英

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!

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM