【问题标题】:HTTP and HTTPS BindingsHTTP 和 HTTPS 绑定
【发布时间】:2012-11-16 12:24:56
【问题描述】:

我有一个使用 WCF Web 服务的应用程序。

我已将其配置为使用 https 绑定。它工作正常。我还想为它添加一个 HTTP 绑定。

我的要求是,如果我的客户端在我的域(或 VPN)上,我需要他们使用 HTTP 绑定。如果他们是外部客户端,我希望他们使用 HTTPS 绑定。我该怎么做呢?我对 WCF 很陌生,这是我第一次尝试它。

我已附上我的配置文件以供参考。我不知道在哪里添加 HTTP 绑定的端点。如有任何帮助,我们将不胜感激。

<system.serviceModel>
    <services>
      <service name="{service name}"
                behaviorConfiguration="httpsBehavior">
        <!--HTTPS END POINT-->
        <endpoint address=""
                  binding="basicHttpBinding"
                  bindingConfiguration="SecureHTTPBinding"
                  contract="{service contract}" />

    <!--METADATA ENDPOINT--> 
    <endpoint address="mex"
              binding="mexHttpsBinding"
              contract="IMetadataExchange" />
  </service>
</services>

<behaviors>
  <serviceBehaviors>
    <behavior name="httpsBehavior">
      <serviceMetadata httpsGetEnabled="true" httpsGetUrl="" />
      <serviceDebug includeExceptionDetailInFaults="false"/>
    </behavior>

    <behavior name="httpBehavior">
      <serviceMetadata httpGetEnabled="true"/>
      <serviceDebug includeExceptionDetailInFaults="false"/>
    </behavior>

  </serviceBehaviors>
</behaviors>

<bindings>
  <basicHttpBinding>
    <!--HTTPS BINDING-->
    <binding name="SecureHTTPBinding">
      <security mode="Transport">
        <transport clientCredentialType="None" />
      </security>
    </binding>

    <!--HTTP BINDING-->
    <binding name="BasicHttpBinding">
      <security mode="None" />
    </binding>

  </basicHttpBinding>
</bindings>

编辑:添加 netTCP 绑定

<system.serviceModel>
    <services>
      <service name="{Service Name}"
                behaviorConfiguration="httpsBehavior">
        <!--HTTPS END POINT-->
        <endpoint address="https://localhost/Service1.svc"
                  binding="basicHttpBinding"
                  bindingConfiguration="SecureHTTPBinding"
                  contract="{Service Contract}"  name="httpsEndPoint"/>

    <!--METADATA ENDPOINT--> 
    <endpoint address="mex"
              binding="mexHttpsBinding"
              contract="IMetadataExchange" />

    <!--HTTP END POINT-->
    <endpoint address="http://localhost/Service1.svc"
              binding="basicHttpBinding"
              bindingConfiguration="HttpBinding"
              contract="{Service Contract}" name="httpsEndPoint"/>

    <!--TCP METATADATA END POINT-->
    <endpoint address="mextcp" binding="mexTcpBinding" contract="IMetadataExchange"/>
    <host>
      <baseAddresses>
        <add baseAddress="net.tcp://localhost:808/Service1.svc" />
      </baseAddresses>
    </host>

    <!--TCP ENDPOIN-->
    <endpoint address="net.tcp://localhost:808/Service1.svc" binding="netTcpBinding" contract="{Service Contract}" name="netTCPEndPoint" />



  </service>
</services>

<behaviors>
  <serviceBehaviors>
    <behavior name="httpsBehavior">
      <serviceMetadata httpsGetEnabled="true" httpsGetUrl="" />
      <serviceDebug includeExceptionDetailInFaults="true"/>
    </behavior>

  </serviceBehaviors>
</behaviors>

<bindings>
  <basicHttpBinding>
    <!--HTTPS BINDING-->
    <binding name="SecureHTTPBinding" allowCookies="true"
             maxReceivedMessageSize="20000000"
             maxBufferSize="20000000"
             maxBufferPoolSize="20000000">
      <readerQuotas maxDepth="32"
             maxArrayLength="200000000"
             maxStringContentLength="200000000"/>

      <security mode="Transport">
        <transport clientCredentialType="None" />
      </security>
    </binding>

    <!--HTTP BINDING-->
    <binding name="HttpBinding" allowCookies="true"
             maxReceivedMessageSize="20000000"
             maxBufferSize="20000000"
             maxBufferPoolSize="20000000">
      <readerQuotas maxDepth="32"
             maxArrayLength="200000000"
             maxStringContentLength="200000000"/>
      <security mode="None" />
    </binding>

  </basicHttpBinding>

  <!--TCP BINDING-->
  <netTcpBinding>
    <binding transferMode="Buffered" />
  </netTcpBinding>

</bindings>

我知道代码中的数字可能会受到 Dos 攻击,我会解决它。现在只是想让它工作。当我使用这个配置文件时,我遇到了错误提示

找不到与端点 MetadataExchageTcpBinding 的方案 net.tcp 匹配的基地址。注册基地址为 [http,https]

谁能帮我为我的服务公开一个 net.tcp 绑定。 任何帮助都会得到帮助

【问题讨论】:

  • 您如何识别客户在您的内部域中?
  • 我使用系统的“环境”变量告诉我用户所在的域
  • @Ron:您是在 IIS、WAS 还是您自己的服务主机代码中托管服务?如果是 IIS,是什么版本?
  • 我会将它托管在 IIS 7 上。为了测试,我将 IIS 8 与 Windows 8 一起使用。谢谢
  • 好的,很好,这让您可以灵活地使用任何您喜欢的绑定。

标签: c# .net wcf wcf-binding


【解决方案1】:

为您的域客户端使用netTcpBinding。默认情况下,您的网络防火墙几乎肯定会阻止原始 TCP 流量,同时允许 HTTP 流量,因此它自然会确保每个客户端都转到适当的端点。您很可能会通过讨价还价为域客户获得额外的性能提升。

如果您不想这样做(例如,您的 WCF 主机不支持它,或者您的 VPN 客户端有问题),那么只需为所有客户端公开一个 HTTPS 端点,无论位置如何。与首先进行跨网络调用的开销相比,SSL 的成本将微不足道。

背景:

  • netTcpBinding 以紧凑的二进制格式传输数据,这比通过 HTTP 发送 XML 文本更有效。缺点是:它与非.NET 客户端不兼容,您必须在防火墙上打开特定端口才能让它通过。不过,这些问题都不会成为您的方案的问题。
  • 您可以在 WCF 服务中公开任意数量的端点,只要它们都具有地址/绑定/合同的唯一不同组合。因此,您可以将 HTTPS 和 TCP 端点暴露给同一个服务,因为它们将具有不同的地址和绑定。

【讨论】:

  • 克里斯蒂安,感谢您的回复。我对 WCF 比较陌生,并不完全了解 netTCPBinding。一开始我对使用两个端点持怀疑态度。您能否详细说明一下有助于我深入了解的答案?再次感谢
  • 这给了我一个很好的主意。谢谢。但我有个问题。如果我要为内部客户端使用 netTCPBinding,我会在配置文件中的哪里添加端点。让我感到困惑的是,我看到 标记的行为定义为“httpsbehavior”。所以当我需要添加一个 TCP 端点时,我需要添加一个新的服务标签吗?谢谢
  • 您为每个.svc 文件(即每个服务类)定义一个&lt;service&gt; 元素。在里面你有任意数量的&lt;endpoint&gt;s,都以各种不同的方式公开该服务。见msdn.microsoft.com/en-us/library/ms731303.aspx
  • Christian,感谢您的帮助,我能够按照您的指导公开 HTTP 和 HTTPS 绑定!你摇滚..
  • 我仍然遇到 net.tcp 的问题。编辑问题以添加我的代码。有什么指导方针吗??
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-10-15
  • 2011-09-05
  • 2018-09-05
  • 2011-04-02
  • 2021-11-10
  • 1970-01-01
  • 2016-10-30
相关资源
最近更新 更多