首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >用WCF生成SOAP请求(RSA-SHA256 PKCS #1 v1.5)

用WCF生成SOAP请求(RSA-SHA256 PKCS #1 v1.5)
EN

Stack Overflow用户
提问于 2019-02-08 10:39:39
回答 1查看 544关注 0票数 1

我需要一些帮助来使用使用SOAP的web服务。我的应用程序使用.NET 4.0。

SOAP请求必须遵循以下请求:

  • 传输协议: HTTPS
  • 加密和身份验证是通过SSL v3/TLS v1.0进行的。
  • 这条消息需要签名。(WS-Security1.1,http://www.w3.org/TR/2002/REC-xml-exc-c14n-20020718,PKCS#1 v1.5,RSA-SHA256)

我必须使用两种不同的证书进行加密和签名。

WCF配置应该是可配置的(签名可以取消激活)。所以绑定必须用c#代码创建,而不是在app.config中创建。

服务器期望的请求示例:

代码语言:javascript
复制
<s:Envelope xmlns:s="http://www.w3.org/2003/05/soap-envelope">
   <s:Header>
      <wsse:Security xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd" xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd">
         <ds:Signature xmlns:ds="http://www.w3.org/2000/09/xmldsig#" Id="SIG-C6D119F21B41F79DBF154885449980234">
            <ds:SignedInfo>
               <ds:CanonicalizationMethod Algorithm="http://www.w3.org/2001/10/xml-exc-c14n#">
                  <ec:InclusiveNamespaces xmlns:ec="http://www.w3.org/2001/10/xml-exc-c14n#" PrefixList="s" />
               </ds:CanonicalizationMethod>
               <ds:SignatureMethod Algorithm="http://www.w3.org/2001/04/xmldsig-more#rsa-sha256" />
               <ds:Reference URI="#id-5">
                  <ds:Transforms>
                     <ds:Transform Algorithm="http://www.w3.org/2001/10/xml-exc-c14n#">
                        <ec:InclusiveNamespaces xmlns:ec="http://www.w3.org/2001/10/xml-exc-c14n#" PrefixList="" />
                     </ds:Transform>
                  </ds:Transforms>
                  <ds:DigestMethod Algorithm="http://www.w3.org/2001/04/xmlenc#sha256" />
                  <ds:DigestValue>...</ds:DigestValue>
               </ds:Reference>
            </ds:SignedInfo>
            <ds:SignatureValue>...</ds:SignatureValue>
            <ds:KeyInfo Id="KI-C6D119F21B41F79DBF154885449979232">
               <wsse:SecurityTokenReference wsu:Id="STR-C6D119F21B41F79DBF154885449979233">
                  <ds:X509Data>
                     <ds:X509IssuerSerial>
                        <ds:X509IssuerName>CN=..,O=...,C=..</ds:X509IssuerName>
                        <ds:X509SerialNumber>...</ds:X509SerialNumber>
                     </ds:X509IssuerSerial>
                  </ds:X509Data>
               </wsse:SecurityTokenReference>
            </ds:KeyInfo>
         </ds:Signature>
      </wsse:Security>
   </s:Header>
   <s:Body xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd" wsu:Id="id-5">
      ...
   </s:Body>
</s:Envelope>

我的第一次尝试是使用WCF生成和发送请求,但我没有找到如何根据需求生成一些内容。然后,我尝试手动生成签名,并使用IClientMessageFormatter和IEndpointBehavior手动创建标题。此解决方案不起作用,因为WCF应用了处理方法(切换xml属性和命名空间.)使签名无效。我最后一次尝试是完全删除WCF并手动发送请求,但是HttpClient在.NET 4.0中是不可用的,而且我也没有找到如何在没有它的情况下发送TLS请求。

有人能告诉我如何配置WCF以生成正确的SOAP请求吗?如果不能用WCF创建请求,如何用.NET 4.0发送TLS请求(并处理响应)?

谢谢。

EN

回答 1

Stack Overflow用户

发布于 2019-02-13 08:08:35

据我所知,头可以在web.config或app.config中配置。

代码语言:javascript
复制
<endpoint address="http://ws-wuxipc-5077:4000/calculator" binding="basicHttpBinding"
  contract="ServiceInterface.ICalculatorService" name="cal">
  <headers>
    <Security xmlns="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd">
      <wsse:UsernameToken xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd"
        xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd">
        <wsse:Username>
        </wsse:Username>
        <wsse:Password Type="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordDigest">monMonDePasse</wsse:Password>
        <wsse:Nonce>sdsdsdlojhfdsdM5Nw==</wsse:Nonce>
        <wsu:Created>2019-01-21T6:17:34Z</wsu:Created>
      </wsse:UsernameToken>
    </Security>
  </headers>
</endpoint>

您还可以通过xml在代码中添加标题。

代码语言:javascript
复制
 using (ChannelFactory<ICalculatorService> ChannelFactory = new ChannelFactory<ICalculatorService>("cal"))
        {
            // ChannelFactory.Endpoint.EndpointBehaviors.Add(new MyEndpointBehavior());
            ICalculatorService employeeService = ChannelFactory.CreateChannel();
            using (OperationContextScope scope = new OperationContextScope((IContextChannel)employeeService))
            {

                System.Xml.XmlDocument document = new XmlDocument();


                XmlElement element = document.CreateElement("wsse", "UsernameToken", "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd");


                XmlElement newChild = null;

                newChild = document.CreateElement("wsse", "Username", "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd");
                newChild.InnerText = "finance";
                element.AppendChild(newChild);

                newChild = document.CreateElement("wsse", "password", "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd");
                newChild.SetAttribute("Type", "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordDigest");
                newChild.InnerText = "387";
                element.AppendChild(newChild);

                MessageHeader messageHeader = MessageHeader.CreateHeader("security", "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd", element, false);


                OperationContext.Current.OutgoingMessageHeaders.Add(messageHeader);
                employeeService.Add(5, 6);
            }

            // List<Employee> list=  employeeService.GetList();
            Console.Read();
        }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54590622

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档