使用Http和Https的WebHttpBinding

14

我正试图为这个网站使用 https 和 http。这个网站有 .svc 文件,它们充当 REST 服务,并从 JavaScript 中调用。

我的配置:

<system.serviceModel>
    <behaviors>
      <endpointBehaviors>
        <behavior name="AjaxBehavior">
          <webHttp />
        </behavior>
      </endpointBehaviors>
      <serviceBehaviors>
        <behavior name="MyServiceBehaviour">
          <serviceDebug includeExceptionDetailInFaults="true" />
          <serviceMetadata httpGetEnabled="True" httpsGetEnabled="true"/>         
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" />
    <services>
      <service behaviorConfiguration="MyServiceBehaviour" name="MyService.Lookups">
        <endpoint address="" behaviorConfiguration="AjaxBehavior"
          binding="webHttpBinding" bindingConfiguration="httpWebBinding" contract="MyService.Lookups" >         
        </endpoint>
        <endpoint address="" behaviorConfiguration="AjaxBehavior"
          binding="webHttpBinding" bindingConfiguration="httpsWebBinding" contract="MyService.Lookups" >          
        </endpoint>
        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
      </service>
    </services>
    <bindings>      
      <webHttpBinding>
        <binding name="httpsWebBinding">
          <security mode="Transport">
            <transport clientCredentialType="None" proxyCredentialType="None" />
          </security>
        </binding>
        <binding name="httpWebBinding">
          <security mode="None">
            <transport clientCredentialType="None" proxyCredentialType="None" />
          </security>
        </binding>
      </webHttpBinding>
    </bindings>   
  </system.serviceModel>

浏览https://myserver/services/Lookups.svc/Hello,结果为

找不到与 WebHttpBinding 绑定的端点的 http 基址。已注册的基址方案为 [https]

浏览http://myserver/services/Lookups.svc/Hello,结果为

找不到与 WebHttpBinding 绑定的端点的 https 基址。已注册的基址方案为 [http]

如果我移除任何一个端点,它将正常工作。例如,删除使用bindingConfiguration="httpWebBinding"配置的端点可用于 HTTPS,

如何使其同时在 HTTP 和 HTTPS 下工作?目前,我只能通过移除一个端点来使用 http 或 https。

参考如何将 WCF 服务配置合并为一个 web.config 中的 http 和 https?如何设置 HTTP 和 HTTPS WCF 4 RESTful 服务?

注意:在 IIS 中,有两个网站,一个监听 http,另一个监听 https。它们共享同一物理文件夹中的代码。

更新:目前,我移除了端点并且它可以工作。但是我担心移除配置有 behaviourConfiguration 的端点并不是最好的解决方案。

这适用于 http 和 https

  <services>
      <service behaviorConfiguration="MyServiceBehaviour" name="MyService.Lookups">

      </service>
    </services>

1
有了 httpsGetEnabled="true",必须有一个 https 的网址,但是你没有定义任何一个。 - Amit Kumar Ghosh
4个回答

8
我已经重新建立了您的场景,并使用您的web.config配置端点进行测试服务。您的配置是正确的,并且可以正常工作。对您而言不起作用的部分可能是您在IIS中的https配置。请确保已启用对您的服务的https访问。如果您使用Visual Studio中的IISExpress进行测试,请单击鼠标左键选择项目并在属性窗口(视图 -> 属性窗口)中选择SSL Enabled = True。

是的。在服务器上,我们创建了 https 绑定并放置了自签名证书。由于我们没有使用双向身份验证,因此我们没有在 IIS 8.5 的 SSL 设置中启用 RequireSSL 标志。 - Billa

1
正如@Lesmian在他的回答中指出的那样,问题出在您的IIS配置中。更具体地说,在以下位置:
注意:在IIS中,有两个网站,一个侦听http,另一个侦听https。两者在物理文件夹中共享相同的代码
原因是IIS无法处理不支持的模式上的端点。您有两个站点,其中一个具有HTTP绑定但没有HTTPS,而另一个具有HTTPS但没有HTTP。
因此,当您浏览http URL时,IIS会将您重定向到(惊喜!)启用http的站点,读取web.config,看到它注册了不受站点支持的https端点,并抛出异常,告诉您该站点上没有https方案支持。
当您浏览https URL时,情况类似-IIS不允许您在仅启用https的站点上使用http端点。
为了处理此问题,最好使用具有两个绑定的单个站点。
另一个(更复杂)的选项是为不同的站点使用不同的web.config:设置单独的站点(指向单独的文件夹),并使用Visual Studio的发布和web.config转换工具。

0

添加这段代码。

<protocolMapping>
  <add scheme="http"  binding="webHttpBinding" bindingConfiguration="httpWebBinding"/>
  <add scheme="https" binding="webHttpBinding" bindingConfiguration="httpsWebBinding"/>
</protocolMapping>

-1

我不知道这个查询是否仍然有效,但是根据我的检查,请使用不同的端点添加binding="mexHttpsBinding"binding="mexHttpBinding"

这个方法对我很有帮助。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接