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

时间:2011-12-27 18:05:25

标签: c# wcf

我正在尝试在我的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请求未经授权,客户端身份验证方案'匿名'。从服务器收到的身份验证标头是'Basic realm = ...” 远程服务器返回错误:(401)未经授权。

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

1 个答案:

答案 0 :(得分:1)

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

查看此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