内部和外部的WCF安全性外部客户

时间:2014-01-24 20:00:07

标签: wcf wcf-binding wcf-security

我计划发布我的Web服务,以便可以在外部网络上使用。出现的问题是,Web管理员不希望将此Web服务外部化,而是希望我在其周围包装代理并使代理服务于外部。我不喜欢代理部分,因为不得不在服务和代理上维护版本控制。我想知道是否有一种简单的方法可以做到这一点,如果您使用外部网络(URL)访问Web服务以使用SSL并请求身份验证,但是如果您在内部网络(URL)中使用它而不请求身份验证或SSL。我尝试在webconfig中有两个端点配置,一个是安全的,一个是不安全的,但问题是当您使用Web服务时,两个绑定都显示,客户端可以选择其中一个或两个。如果有人以不同的方式做到这一点或者以不同的方式让我知道,通常我会采用一种方法,无论是全部安全还是非安全,但根据网络不同。非常感谢:)

1 个答案:

答案 0 :(得分:1)

您可能会知道WCF内置了对“代理”的支持。它被称为路由服务,可在WCF 4.0及更高版本中使用。

您可以将其配置为将特定服务合同的互联网呼叫路由到Intranet中运行的WCF服务。它甚至可以转换绑定,以便外部客户可以使用可以通过防火墙的HTTP绑定来调用使用TCP绑定的内部服务。

只需知道要路由到哪些合同。因此,当您的合同发生变化时,无需更新它......

有关详细信息,请参阅here

已编辑:以下示例system.serviceModel节点允许客户端向路由服务添加服务引用。诀窍是让路由服务将其自身强加为它所路由到的服务。请参阅serviceActivations节点。请注意,这样就无需使用.svc文件。

接下来,我们定义两个端点过滤器,将服务请求重定向到路由服务,并将mex端点(公开元数据)的请求重定向到路由服务的mex端点。请参阅filters节点。

最后,我们明确禁止通过http公开路由服务本身的元数据,以强制客户端使用mex(我们正在路由)进行发现。请参阅serviceBehaviors节点。

再次编辑:为mex尺寸限制添加了解决方案

<system.serviceModel>
  <serviceHostingEnvironment>
    <serviceActivations>
      <!--Lets the routing service impose himself as Service.svc  (No .SVC file is needed!!!) -->
      <add service="System.ServiceModel.Routing.RoutingService" relativeAddress="Service.svc" />
    </serviceActivations>
  </serviceHostingEnvironment>
  <bindings>
    <wsHttpBinding>
      <!-- a mexHttpBinding is in fact a wsHttpBinding with security turned off -->
      <binding name="mexBinding" maxReceivedMessageSize="5000000">
        <security mode="None"/>
      </binding>
    </wsHttpBinding>
  </bindings>
  <behaviors>
    <serviceBehaviors>
      <behavior >
        <!-- Use the filter table with the name 'Filters' defined below -->
        <routing routeOnHeadersOnly="false" filterTableName="Filters"/>
        <serviceDebug includeExceptionDetailInFaults="true"/>
        <!-- Disable exposing metadata for this routing service to force discovery using mex -->
        <serviceMetadata httpGetEnabled="false"/>
      </behavior>
    </serviceBehaviors>
  </behaviors>
  <routing>
    <filters>
      <!-- Declare a routing filter that filters on endpoint Service.svc -->
      <filter name="WcfServiceFilter" filterType="EndpointAddress" filterData="http://localhost/ServiceRouter/Service.svc" />
      <!-- Declare a routing filter that filters on mex endpoint of Service.svc -->
      <filter name="WcfServiceFilter.mex" filterType="EndpointAddress" filterData="http://localhost/ServiceRouter/Service.svc/mex"/>
    </filters>
    <filterTables>
      <!-- Declare the routing table to use -->
      <filterTable name="Filters">
        <!-- requests that match the WcfServiceFilter (declared above) should be routed to the client endpoint WcfService -->
        <add filterName="WcfServiceFilter" endpointName="WcfService"/>
        <!-- requests that match the WcfServiceFilter.mex (declared above) should be routed to the client endpoint WcfService.mex -->
        <add filterName="WcfServiceFilter.mex" endpointName="WcfService.mex"/>
      </filterTable>
    </filterTables>
  </routing>
  <services>
    <!-- Declare our service instance and the endpoints it listens on -->
    <service name="System.ServiceModel.Routing.RoutingService">
      <!-- Declare the endpoints we listen on -->
      <endpoint name="WcfService" contract="System.ServiceModel.Routing.IRequestReplyRouter" binding="wsHttpBinding" />
      <endpoint name="WcfServiceFilter.mex" address="mex" contract="System.ServiceModel.Routing.IRequestReplyRouter" binding="mexHttpBinding" />
    </service>
  </services>
  <client>
    <!-- Define the client endpoint(s) to route messages to -->
    <endpoint name="WcfService" address="http://localhost/WcfService/Service.svc" binding="wsHttpBinding" contract="*" />
    <endpoint name="WcfService.mex" address="http://localhost/WcfService/Service.svc/mex" binding="wsHttpBinding" bindingConfiguration="mexBinding" contract="*" />
  </client>
</system.serviceModel>
相关问题