使用证书和用户/通行证从.Net获取Java Web服务

时间:2013-08-16 18:28:01

标签: wcf-binding wcf-security

我有一个.Net c#客户端需要使用来自第三方的Java Web服务。它们需要客户端证书和用户名和密码。我已经设置了证书,但不断获得401 Unauthorized,因为我认为用户名和密码实际上并未附加到请求中。看起来WCF期望一个或另一个但不是证书和用户名/密码。当然我错过了什么。

<bindings>
<binding name="CC2WebSoap">
  <security mode="Transport">
    <transport clientCredentialType="Certificate" />
  </security>
</binding>
</bindings>
<client>
      <endpoint address="https://url_goes_here.com"
                binding="basicHttpBinding" 
                bindingConfiguration="CC2WebSoap"
                contract="acontract" 
                name="CC2WebSoap" 
                behaviorConfiguration="SecureClientBehavior"/>
</client>
 <behaviors>
      <endpointBehaviors>        
        <behavior name="SecureClientBehavior">
          <clientCredentials>
            <clientCertificate findValue="mythumbprint" storeLocation="LocalMachine" storeName="My" x509FindType="FindByThumbprint"/>
          </clientCredentials>
        </behavior>
      </endpointBehaviors>
    </behaviors>

     try
            {
        CC2WebSoap client= new CC2WebSoapClient("CC2WebSoap");
                client.ClientCredentials.UserName.UserName = "username";
                client.ClientCredentials.UserName.Password = "password";

                request = BuildRequest();

                response = client.DoSomething(request);
}
catch(Exception e){ // Always get 401 exception here. }

1 个答案:

答案 0 :(得分:0)

通过添加MessageInspector和相关类以使WCF在每个请求之前将用户名和密码附加到标头,结果相当简单。具体来说,我完全按照以下博客文章中的建议。

Using a MessageInspector To modify HTTP Headers