繁体   English   中英

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

[英]How to supply both UserName and Client Certificate in WCF client (why does this example work)?

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

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

WCF客户端证书和用户名凭据被禁止

我可以在某种程度上理解那里发生了什么,并意识到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服务。

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

服务器配置:

  <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客户端证书和用户名凭据被禁止

以及这一个:

将非BizTalk WCF配置转换为BizTalk WCF-Custom端点

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM