通過配置web.config使WCF向外提供HTTPS的Restful Service

Eric Sun發表於2015-01-26

如何通過WCF向外提供Restful的Service請看如下連結

http://www.cnblogs.com/mingmingruyuedlut/p/4223116.html

 

那麼如何通過對web.config的配置,使原有的Service即符合HTTP又符合HTTPS呢?

請看如下具體步驟:

1):將上篇文章 http://www.cnblogs.com/mingmingruyuedlut/p/4223116.html 中的IIS對應的Site繫結80埠,並且新增HTTPS的443埠

 

2):保持其他不變,然後我們訪問 http://localhost/UserService.svc 依舊可以看到我們所提供的Service是好用的,現在我們向web.config檔案新增binding來實現HTTPS的配置

2.1):在system.ServiceModel節點中新增如下節點

    <bindings>
      <webHttpBinding >
        <binding name="SecureWebBinding" >
            <security mode="Transport">
              <transport clientCredentialType="None"></transport>
            </security>
        </binding>
      </webHttpBinding>
     </bindings>

 

2.2):然後在services節點中新增如下節點

          <endpoint address=""
                  binding="webHttpBinding"
                  bindingConfiguration="SecureWebBinding"
                  contract="EricSunWcfService.IUserService"
                  behaviorConfiguration="ESEndPointBehavior"/>
        
          <endpoint address="mex" binding="mexHttpsBinding" contract="IMetadataExchange"/>

 

2.3):最終的配置檔案為

<?xml version="1.0"?>
<configuration>

  <appSettings>
    <add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />
  </appSettings>
  <system.web>
    <compilation debug="true" targetFramework="4.5" />
    <httpRuntime targetFramework="4.5"/>
  </system.web>
  <system.serviceModel>
    <services>
        <service name="EricSunWcfService.UserService" behaviorConfiguration="RESTBehaviour">
          <endpoint address=""
                    binding="webHttpBinding"
                    contract="EricSunWcfService.IUserService"
                    behaviorConfiguration="ESEndPointBehavior"/>
          <endpoint address=""
                  binding="webHttpBinding"
                  bindingConfiguration="SecureWebBinding"
                  contract="EricSunWcfService.IUserService"
                  behaviorConfiguration="ESEndPointBehavior"/>
        
          <endpoint address="mex" binding="mexHttpsBinding" contract="IMetadataExchange"/>
        </service>
      </services>
    <behaviors>
      <serviceBehaviors>
        <behavior name="RESTBehaviour">
          <serviceMetadata httpGetEnabled="true"/>
          <serviceDebug includeExceptionDetailInFaults="true"/>
        </behavior>
        
        <behavior>
          <!-- To avoid disclosing metadata information, set the values below to false before deployment -->
          <serviceMetadata httpGetEnabled="true" 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="ESEndPointBehavior">
          <webHttp/>
        </behavior>
      </endpointBehaviors>
    </behaviors>
    <protocolMapping>
        <add binding="basicHttpsBinding" scheme="https" />
    </protocolMapping>    
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
    <bindings>
      <webHttpBinding >
        <binding name="SecureWebBinding" >
            <security mode="Transport">
              <transport clientCredentialType="None"></transport>
            </security>
        </binding>
      </webHttpBinding>
     </bindings>
  </system.serviceModel>
  <system.webServer>
    <!--
      To browse web app root directory during debugging, set the value below to true.
      Set to false before deployment to avoid disclosing web app folder information.
    -->
    <directoryBrowse enabled="true"/>
    <!--<modules runAllManagedModulesForAllRequests="true"/>-->

    <modules runAllManagedModulesForAllRequests="true">
      <remove name="WebDAVModule" />
    </modules>
    <handlers>
      <remove name="WebDAV" />
    </handlers>
  
  </system.webServer>

</configuration>
View Code

 

3):這樣我們通過URL-->  https://localhost/UserService.svc ,以及 http://www.cnblogs.com/mingmingruyuedlut/p/4223116.html 這篇文章中的方法去測試我們提供的Service是好用的

 

至此如何新增https的訪問節點就搞定了~~ 

 

更多資訊請看如下連結:

https://nirajrules.wordpress.com/2011/04/29/wcf-rest-over-https/ 

 

相關文章