【问题标题】:Call HTTPS REST service from WCF从 WCF 调用 HTTPS REST 服务
【发布时间】:2011-12-20 17:00:47
【问题描述】:

我需要通过 HTTPS 从第三方服务器调用一些 REST 服务。我的想法是,我将为自己创建一个不错的小型 WCF 库来处理这些调用并反序列化响应。不过,我有点不高兴了。

我尝试调用的服务有一个简单响应 OK 的测试服务。

我已经在我的界面中创建了一个 OperationContract,如下所示:

[OperationContract]
[WebGet(BodyStyle = WebMessageBodyStyle.Bare,
    ResponseFormat = WebMessageFormat.Xml,
    UriTemplate = "test")]
 string Test();

在我的服务代码中,我有一个公共方法如下:

public string Test()
{
    ChannelFactory<IService1> factory = new ChannelFactory<IService1>("UMS");
    var proxy = factory.CreateChannel();
    var response = proxy.Test();
    ((IDisposable)proxy).Dispose();
    return (string)response;
}

我的 app.config 如下所示:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <system.web>
    <compilation debug="true" />
  </system.web>
  <system.serviceModel>
    <client>
      <endpoint address="https://61.61.34.19/serv/ums/"
              binding="webHttpBinding"
              behaviorConfiguration="ums"
              contract="WCFTest.IService1"
              bindingConfiguration="webBinding"
              name="UMS" />
    </client>
    <services>
      <service name="WCFTest.Service1" behaviorConfiguration="WCFTest.Service1Behavior">
        <host>
          <baseAddresses>
            <add baseAddress = "http://localhost:8731/Design_Time_Addresses/WCFTest/Service1/" />
          </baseAddresses>
        </host>
        <endpoint address ="" binding="wsHttpBinding" contract="WCFTest.IService1">
          <identity>
            <dns value="localhost"/>
          </identity>
        </endpoint>
        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
      </service>
    </services>
    <behaviors>
      <endpointBehaviors>
        <behavior name="ums">
          <clientCredentials>
            <clientCertificate findValue="restapi.ext-ags.801" 
                               storeLocation="CurrentUser" 
                               x509FindType="FindBySubjectName"/>
          </clientCredentials>
          <webHttp/>
        </behavior>
      </endpointBehaviors>
      <serviceBehaviors>
        <behavior name="WCFTest.Service1Behavior">
          <serviceMetadata httpGetEnabled="True"/>
          <serviceDebug includeExceptionDetailInFaults="False" />
        </behavior>       
      </serviceBehaviors>
    </behaviors>
    <bindings>
      <webHttpBinding>
        <binding name="webBinding">
          <security mode="Transport">
            <transport clientCredentialType="Certificate" proxyCredentialType="None"/>
          </security>
        </binding>
      </webHttpBinding>
    </bindings>
  </system.serviceModel>
</configuration>

我尝试调用测试方法,但收到错误“无法为具有权限 '61.61.34.19' 的 SSL/TLS 安全通道建立信任关系。”

谁能看到我在这里遗漏了什么?

任何帮助表示赞赏。干杯。

【问题讨论】:

  • 您能否从您的 IE 浏览到 REST 服务并查看响应? REST 服务是否使用客户端证书完成客户端身份验证?
  • 是的 Rajesh,我可以从 IE 浏览并查看响应。
  • 如果您的证书是自签名的,那么您是否实现了验证服务器证书的 ServicePointManager 回调方法。此外,您是否从您尝试测试的客户端计算机浏览到服务?
  • H 拉杰什。证书由我需要使用其服务的第 3 方提供。我在我的开发机器上,服务在其他地方的服务器上。
  • 您是否确保在从您的计算机发出请求时发送客户端证书?能不能在测试方法中做到这一点--> factory.Credentials.ClientCertificate = "your client cert" 然后看看你的请求是否成功

标签: wcf https


【解决方案1】:

无法为具有权限“61.61.34.19”的 SSL/TLS 安全通道建立信任关系。

这通常意味着服务器的 SSL 证书存在问题。是自签的吗?如果是这样,您必须建立对证书的信任,通常是通过trusting the issuing CA or installing the cert in the windows cert store

【讨论】:

  • 干杯 Randolph,证书已安装在商店中,如果我在 IE 中点击测试服务的 URI,我可以看到结果,只是努力从 WCF 执行相同的操作。还尝试通过询问证书存储、定位证书并添加到 HttpWebRequest.CientCertificates 集合然后调用 .GetResponse() 作为 HttpWebResponse 直接从沼泽标准 WPF 应用程序调用
【解决方案2】:

尝试将 webHttpBinding 安全设置从“传输”更改为“无”。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-03-31
    • 1970-01-01
    • 2010-11-19
    • 1970-01-01
    • 2012-01-29
    • 2010-09-21
    • 2010-12-12
    • 1970-01-01
    相关资源
    最近更新 更多