使用X509证书时如何配置WCF客户端绑定

本文关键字:配置 WCF 客户端 绑定 证书 X509 何配置 使用 | 更新日期: 2023-09-27 18:18:49

我正在尝试在WCF客户端中设置凭据,如下所示。

            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);

wcf客户端绑定设置如下:

    <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>

我收到一个错误" http请求是未经授权的客户端身份验证方案'匿名'。从服务器接收到的身份验证头是"基本域=…"远程服务器返回一个错误:(401)Unauthorized.

任何帮助都是感激的。我不确定在我的app.config文件的绑定部分需要改变什么。谢谢!

使用X509证书时如何配置WCF客户端绑定

看起来您的错误可能在传输节点

查看MSDN页面中的参考

是一个例子。

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

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

HttpBinding MSDN