使用SSL和HTTP身份验证在WCF客户端中使用哪种安全模式?

时间:2012-01-10 15:47:18

标签: wcf wcf-security wcf-binding wcf-client

我需要在我的WCF客户端中使用SSL和HTTP身份验证才能连接到Web服务。我有如下所示的wcf客户端绑定配置和加载证书的代码,并传递http授权的用户名和密码。当我尝试这个时,我收到一个错误“http请求未经授权使用客户端身份验证方案'匿名'。来自服务器的身份验证是'Basic realm =”ws.dataway.com:443'“。远程服务器返回了错误:(401)未经授权。任何人都可以告诉如何解决这个问题以及绑定配置中需要更改的内容?谢谢!

            <basicHttpBinding>
            <binding name="OrderBinding" 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="Certificate" proxyCredentialType="None"
                        realm="" />
               </security>
            </binding>
            </basicHttpBinding>

代码:

            ClientCredentials loginCredentials = new ClientCredentials();
            loginCredentials.UserName.UserName = this.UserId;
            loginCredentials.UserName.Password = this.Password;
            loginCredentials.ClientCertificate.SetCertificate(StoreLocation.LocalMachine, StoreName.My, X509FindType.FindByIssuerName, "link.com");

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

1 个答案:

答案 0 :(得分:2)

尝试此自定义绑定:

  <customBinding>
    <binding name="Secured">
      <textMessageEncoding messageVersion="Soap11" />
      <httpsTransport authenticationScheme="Basic" 
                      realm="ws.dataway.com:443"
                      requireClientCertificate="true" />
    </binding>
  </customBinding>