如何在WCF RESTful服务上启用HTTPS?

Taj*_*mar 16 c# rest ssl wcf

如何使wcf在https上工作.我想使用这个wcf而不是https我搜索了很多文章我没有得到答案请帮助我对wcf概念的新手.我想从ajax,jquery中调用它

 <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"/>
Run Code Online (Sandbox Code Playgroud)

Der*_*k W 35

看来你正在用WCF构建一个RESTful服务,你真的很接近它.

以下是您需要做的事情来保护它:

  1. 添加WebHttpBinding安全模式设置为的新配置Transport.
  2. 将该新WebHttpBinding配置分配给您的服务端点绑定.
  3. 确保只能通过设置通过HTTPS访问RESTful服务httpGetEnabled="false".
  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>
  </services>
  <!-- Add secure WebHttpBinding config -->
  <bindings>
     <webHttpBinding>
        <binding name="webHttpTransportSecurity">
           <security mode="Transport" />
         </binding>
      </webHttpBinding>
  </bindings>
  <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>
Run Code Online (Sandbox Code Playgroud)

  • 这有助于我,但有一个调整.使用相同的答案我有这个错误`无法识别的元素'绑定'.这是因为<bindings> </ bindings>必须在<services> </ services>之外 (4认同)