如何在WCF客户端中提供UserName和Client Certificate(为什么这个例子有用)?

时间:2014-01-24 20:28:56

标签: c# wcf soap ssl

考虑一种WCF服务,其目的是在传输层需要客户端证书(IIS中的客户端证书设置为“必需”)。同样,消息层将有用户名验证。

现在我已经看到了这个问题:

WCF Client Certificate AND UserName Credentials forbidden

我可以在某种程度上理解那里发生了什么,并意识到WCF本身并不允许这两者。我在代码中执行了与上面引用的链接中的海报相同的步骤,并找到了相同的结果...消息级别的UserName凭据被传递(在我的情况下在SOAP标头中),但客户端证书(尽管是在VS调试中查看请求客户端时附加的,实际上并未由端点处理。

所以现在让我困惑的那部分。我决定稍微破解它。 我想知道为什么它的工作原理与我想要的一样......它超出了IIS客户端证书要求,UserName被传递给WCF服务并且一切正常。然而,WCF不允许我只使用WCF配置文件或代码(我能找到)。为什么?

            // sets up a proxy client based on endpoint config
            // basically just here to get the URL.
            this.InitializeSubmitClient();

            // these get used to create the HttpWebRequest
            string url = this.submitClient.Endpoint.Address.ToString();
            string action = "SubmitCDA";

            // this deserializes an XML file which is the "shell" of SOAP document and inject username/password into SOAP Security node
            XmlDocument soapEnvelopeXml = XMLHelper.CreateSoapDocument(this.txtSubmitCdaXmlFile.Text, this.txtAdAccount.Text, this.txtPassword.Text);
            HttpWebRequest webRequest = XMLHelper.CreateWebRequest(url, action);

            // saves the SOAP XML into the webRequest stream.
            XMLHelper.InsertSoapEnvelopeIntoWebRequest(soapEnvelopeXml, webRequest);

            // attach the cert
            if (this.chkSendClientCert.Checked)
            {
                X509Certificate myCert = X509Certificate.CreateFromCertFile(@"C:\temp\CDX-IHAT_DevClientCert.cer");
                webRequest.ClientCertificates.Add(myCert);
            }
            else
            {
                webRequest.ClientCertificates.Clear();
            }

            // begin async call to web request.
            IAsyncResult asyncResult = webRequest.BeginGetResponse(null, null);

更复杂的是,适用的WCF服务是BizTalk服务。

1 个答案:

答案 0 :(得分:3)

以下是我最终如何做到这一点。

服务器配置:

  <customBinding>
    <binding name="CustomCDARequestEndpointBinding">                    
      <textMessageEncoding messageVersion="Soap11" />
      <security authenticationMode="UserNameOverTransport" />
      <httpsTransport requireClientCertificate="true" />
    </binding>
  </customBinding>

客户端配置:

<system.ServiceModel>
  <bindings>
    <customBindings>
      <binding name="CustomBinding_ITwoWayAsync">
          <security defaultAlgorithmSuite="Default"
            authenticationMode="UserNameOverTransport"
            requireDerivedKeys="true"
            includeTimestamp="true"
            messageSecurityVersion="WSSecurity11WSTrustFebruary2005WSSecureConversationFebruary2005WSSecurityPolicy11BasicSecurityProfile10"
          >
            <localClientSettings detectReplays="false" />
            <localServiceSettings detectReplays="false" />
          </security>
          <textMessageEncoding messageVersion="Soap11" />
          <httpsTransport requireClientCertificate="true" />
      </binding>
    </customBinding>
  </bindings>
  <behaviors>
    <endpointBehaviors>
      <behavior name="ohBehave">
        <clientCredentials useIdentityConfiguration="false">
        <clientCertificate findValue="6D0DBF387484B25A16D0E3E53DBB178A366DA954" storeLocation="CurrentUser"
          x509FindType="FindByThumbprint" />            
        </clientCredentials>          
      </behavior>
    </endpointBehaviors>
  </behaviors>
  <client>
     <endpoint address="https://myservice/CDASubmitService/CDASubmit.svc"
        binding="customBinding" bindingConfiguration="SubmitDev" behaviorConfiguration="ohBehave"
        contract="CDASubmitService.CDASubmit" name="SubmitDev" />
  </client>
</system.serviceModel>

让它运作的关键是<httpsTransport requireClientCertificate="true" />元素和<security authenticationMode="UserNameOverTransport"元素/属性。

此配置允许我完全通过配置文件向WCF(BizTalk)服务提交消息,而不更改实际代码。它仍然允许我提交给VIA WebRequest,如上所示。

我必须赞扬这篇文章:

WCF Client Certificate AND UserName Credentials forbidden

以及这一个:

Translate non-BizTalk WCF config into BizTalk WCF-Custom endpoint

最终让我走上正轨。我总是回避WCF中的自定义绑定,因为我认为它有点矫枉过正,但它们真的没什么了不起的,只是提供比开箱即用更详细的配置的方法。