2015-03-25 44 views
11

如何使wcf通過https工作。我想通過https使用這個wcf我已經搜索了很多文章,我沒有得到答案,請幫助iam新的wcf概念。我想從AJAX調用它,jQuery的如何在WCF RESTful服務上啓用HTTPS?

<system.serviceModel > 
<services> 
    <service 
    name="WcfRestfulService.HttpService" behaviorConfiguration="ServiceBehaviour" > 
    <endpoint address="" binding="webHttpBinding" behaviorConfiguration="web" 
       contract="WcfRestfulService.IHttpService"> 
    </endpoint> 
    </service> 
</services> 
<behaviors> 
    <serviceBehaviors> 
    <behavior name="ServiceBehaviour"> 
     <!-- To avoid disclosing metadata information, set the value below to false and remove the metadata endpoint above before deployment --> 
     <serviceMetadata httpsGetEnabled="true"/> 
     <!-- To receive exception details in faults for debugging purposes, set the value below to true. Set to false before deployment to avoid disclosing exception information --> 
     <serviceDebug includeExceptionDetailInFaults="false"/> 
    </behavior> 
    </serviceBehaviors> 
    <endpointBehaviors> 
    <behavior name="web"> 
     <webHttp/> 
    </behavior> 
    </endpointBehaviors> 
</behaviors> 
<serviceHostingEnvironment multipleSiteBindingsEnabled="true"/> 

+0

你只用希望它'webHttpBinding'或'basichttpbinding'也將達到目的? – 2015-03-25 06:04:35

+0

德里克W公司的答案是完美的,但有一點: 的<綁定>元素不能在裏面<服務> - 之後< /服務>和前<行爲>移動到位。 – Craig 2017-06-13 05:43:10

回答

26

看樣子你正在建設一個RESTful與WCF服務,你是真的關閉來保護它。

以下是你需要做的,以確保它是什麼:

  1. 新增WebHttpBinding配置有設置爲Transport安全模式。
  2. 將新的WebHttpBinding配置分配給您的服務端點綁定。
  3. 確保只能通過設置httpGetEnabled="false"通過HTTPS訪問您的RESTful服務。
  4. 設置元數據發佈端點以使用HTTPS。

這些更改全部歸納在修改後的配置文件中(請參閱已更改點的註釋)。另請注意,您的服務端點必須使用HTTPS方案,而不是HTTP。

<system.serviceModel > 
    <services> 
    <service name="WcfRestfulService.HttpService" 
       behaviorConfiguration="ServiceBehaviour" > 
     <endpoint address="" 
        binding="webHttpBinding" 
        <!-- Add reference to secure WebHttpBinding config --> 
        bindingConfiguration="webHttpTransportSecurity" 
        behaviorConfiguration="web" 
        contract="WcfRestfulService.IHttpService" /> 
     <!-- Need to make sure that our metadata 
       publishing endpoint is using HTTPS as well --> 
     <endpoint address="mex" 
        binding="mexHttpsBinding" 
        contract="IMetadataExchange" /> 
    </service> 
    <!-- Add secure WebHttpBinding config --> 
    <bindings> 
     <webHttpBinding> 
      <binding name="webHttpTransportSecurity"> 
       <security mode="Transport" /> 
      </binding> 
     </webHttpBinding> 
    </bindings> 
    </services> 
    <behaviors> 
     <serviceBehaviors> 
     <behavior name="ServiceBehaviour"> 
      <serviceMetadata httpsGetEnabled="true" 
           <!-- Make sure the service can 
           be accessed only via HTTPS --> 
           httpGetEnabled="false"/> 
      <serviceDebug includeExceptionDetailInFaults="false"/> 
     </behavior> 
     </serviceBehaviors> 
     <endpointBehaviors> 
     <behavior name="web"> 
      <webHttp/> 
     </behavior> 
     </endpointBehaviors> 
    </behaviors> 
    <serviceHostingEnvironment multipleSiteBindingsEnabled="true"/> 
</system.serviceModel> 
+1

這幫助了我,但做了一些調整。使用相同的答案,我有這個錯誤'無法識別的元素'綁定'。這是因爲必須在之外 – Mese 2017-10-12 10:34:14

1

你需要設置security mode="Transport"在結合

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

瞭解更多關於MSDN

+4

使用WCF的RESTful服務需要使用'WebHttpBinding'而不是'BasicHttpBinding.'。 WebHttpBinding是WCF如何以RESTful方式公開服務。您的答案在基於SOAP的服務的上下文中是正確的。 – 2015-03-25 19:10:20

0

我有同樣的問題,但想測試HTTP獲取請求,因爲我的服務是內部的。

還記得讓HTTPS獲得啓用。 httpsGetEnabled="true"

我的配置如下是作爲一個例子:

<bindings > 
     <basicHttpBinding> 
     <binding name="secureHttpBinding" > 
      <security mode="Transport" /> 
     </binding> 
    </bindings> 
    ..... 
    <behaviors> 
     <serviceBehaviors> 
     <behavior > 
      <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/> 
      <serviceDebug includeExceptionDetailInFaults="false"/> 
     </behavior> 
     </serviceBehaviors> 
    </behaviors>