如何在WCF中为HTTPS设置配置?

时间:2013-09-18 04:59:42

标签: .net wcf

我有以下配置文件让WCF在HTTP下完美运行,但不知怎的,这在HTTPS下无效。有谁知道那里出了什么问题?我该如何解决?

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

  <system.web>
    <compilation debug="true" targetFramework="4.0" />
  </system.web>

  <system.serviceModel>




    <serviceHostingEnvironment multipleSiteBindingsEnabled="true" />

    <bindings>
      <!-- if using basicHttpBinding, wsHttpBinding section should be commented -->
      <wsHttpBinding>
        <binding name="wsHttpBindingConfig">
          <security mode="None">
            <transport  clientCredentialType="None" proxyCredentialType="None" />
            <message clientCredentialType="None" negotiateServiceCredential="False" algorithmSuite="Default"/>
          </security>
        </binding>
      </wsHttpBinding>
    </bindings>



    <services>
      <service name="SEWebService.Service1" behaviorConfiguration="SEWebService.Service1Behavior">

        <!-- basicHttpBinding is SOAP-based Service, it sends data in plain text, but it supports maximum types of clients.
        It supports Soap 1.1, lower version of .Net Client, like .Net 1.1 -->
        <!--
        <endpoint address="basic" binding="basicHttpBinding" contract="SearchApi.ISearchApi"/>
         -->

        <!--wsHttpBinding is SOAP-based Service, it sends encrypted data by default, but it needs supporting WS-* specification clients and
        at least .Net3.0 or higher version
        By default  wsHttpBinding uses Windows authentication, we need to turn it off by using bindingConfiguration-->
        <endpoint address="ws" binding="wsHttpBinding" contract="SEWebService.IService1" bindingConfiguration="wsHttpBindingConfig" >
        </endpoint>

        <!-- webHttpBinding is non-SOAP HTTP services, here webHttpBinding register REST to be a Restful WCF Service
        It uses [WebGet] or [WebInvoke] to map an Http Request to a WCF operation-->
        <endpoint address="" binding="webHttpBinding" contract="SEWebService.IService1" behaviorConfiguration="REST">
          <!--Upon deployment, the following identity element should be removed or replaced to reflect the identity under which the deployed service runs. 
          If removed, WCF will infer an appropriate identity automatically.-->
          <identity>
            <dns value="localhost"/>
          </identity>
        </endpoint>


        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>

      </service>
    </services>


    <behaviors>
      <serviceBehaviors>

        <behavior name="SEWebService.Service1Behavior">

          <useRequestHeadersForMetadataAddress>
            <defaultPorts>
              <add scheme="http" port="80" />
              <add scheme="https" port="443" />
            </defaultPorts>
          </useRequestHeadersForMetadataAddress>
          <!-- To avoid disclosing metadata information, set the value below to false before deployment -->
          <serviceMetadata httpGetEnabled="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="REST">
          <webHttp/>
        </behavior>
      </endpointBehaviors>
    </behaviors>

  </system.serviceModel>
  <system.webServer>
    <modules runAllManagedModulesForAllRequests="true"/>
    <!--
        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"/>
  </system.webServer>
  <connectionStrings>
    <add name="DbConnectionString" connectionString="nitial Catalog=Smart_Web_Service;uid=SmartWebService_User;pwd=73E;Connect Timeout=10; pooling=true;" providerName="System.Data.SqlClient" />
  </connectionStrings>
</configuration>

1 个答案:

答案 0 :(得分:0)

查看配置文件

1)将mexHttpBinding更改为 mexHttpsBinding

<endpoint address="mex" binding="mexHttpsBinding" contract="IMetadataExchange"/>

2)在serviceMetadata中将httpGetEnabled更改为 httpsGetEnabled

<serviceMetadata httpsGetEnabled="true"/>

3)启用传输级别安全性

<security mode="Transport">

另请参阅此article以供参考。