Search code examples
c#.netweb-serviceswcfhttps

How to make WCF Service Use HTTPS protocol


I have a WCF service which works as rest and soap. I am trying to receive requests via HTTPS protocol but I can't no matter what I've tried.

I've tried to modify the web.config file according to this post:

How to enable HTTPS in WCF service

I've tried couple of other similar methods I've found on the web. But no matter I do, I always get: "Could not find a base address that matches scheme http for the endpoint with binding WSHttpBinding. Registered base address schemes are [https]." error upon running the project on debug mode.

My configuration is as follows:

  <system.serviceModel>
    <bindings>
      <basicHttpBinding>
        <binding name="SendEmailSoap" />
        <binding name="SendSMSSoap" />
        <binding name="KPSPublicSoap">
          <security mode="Transport" />
        </binding>
        <binding name="KPSPublicSoap1" />
      </basicHttpBinding>

      <webHttpBinding>
        <binding name="WebBinding">
          <security mode="None">
            <transport clientCredentialType="None" />
          </security>
        </binding>
      </webHttpBinding>

      <wsHttpBinding>
        <binding name="WsBinding">
          <security mode="None" />
        </binding>
      </wsHttpBinding>

    </bindings>

    <behaviors>
      <endpointBehaviors>
        <behavior name="web">
          <webHttp />
        </behavior>
      </endpointBehaviors>
      <serviceBehaviors>
        <behavior name="servicebehaviors">
          <serviceMetadata httpGetEnabled="true" />
          <serviceDebug includeExceptionDetailInFaults="false" />
        </behavior>
        <behavior name="">
          <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true" />
          <serviceDebug includeExceptionDetailInFaults="false" />
        </behavior>
        <behavior name="integrationServiceBehaviour">
          <serviceMetadata httpGetEnabled="true" httpsGetEnabled="false" />
          <serviceDebug includeExceptionDetailInFaults="false" />
          <serviceCredentials>
            <userNameAuthentication userNamePasswordValidationMode="Custom" customUserNamePasswordValidatorType="KaporaWebService.App_Code.CustomValidator, App_Code" />
          </serviceCredentials>
        </behavior>

      </serviceBehaviors>
    </behaviors>
    <services>
      <service behaviorConfiguration="integrationServiceBehaviour" name="KaporaWebService.IntegrationService">
        <endpoint address="/ws" binding="wsHttpBinding" bindingConfiguration="WsBinding" contract="KaporaWebService.IIntegrationService" />
        <endpoint address="" behaviorConfiguration="web" binding="webHttpBinding" bindingConfiguration="WebBinding" contract="KaporaWebService.IIntegrationService" />
      </service>
    </services>
    <protocolMapping>
      <add binding="basicHttpsBinding" scheme="https" />
    </protocolMapping>
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="false" />
  </system.serviceModel>

What do I need to do enable HTTPS on WCF?


Solution

  • We are supposed to add additional service endpoint which uses transport layer security, and add https binding in IIS site binding module. Please see my configuration.

    <system.serviceModel>
        <services>
          <service behaviorConfiguration="mybehavior" name="WcfService1.Service1">
            <endpoint address="" binding="webHttpBinding" contract="WcfService1.IService1" behaviorConfiguration="webbev"></endpoint>
            <endpoint address="" binding="webHttpBinding" contract="WcfService1.IService1" behaviorConfiguration="webbev" bindingConfiguration="mybinding"></endpoint>
            <endpoint address="mex" binding="mexHttpsBinding" contract="IMetadataExchange"></endpoint>
          </service>
        </services>
        <bindings>
          <webHttpBinding>
            <binding name="mybinding" maxBufferPoolSize="2147483647" maxReceivedMessageSize="2147483647" maxBufferSize="2147483647" transferMode="Streamed" sendTimeout="00:10:00" receiveTimeout="00:10:00">
              <readerQuotas maxDepth="2147483647" maxStringContentLength="2147483647" maxArrayLength="2147483647" maxBytesPerRead="2147483647" />
              <security mode="Transport">
                <transport clientCredentialType="None"></transport>
              </security>
            </binding>
          </webHttpBinding>
        </bindings>
        <behaviors>
          <serviceBehaviors>
            <behavior name="mybehavior">
              <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true" />
              <serviceDebug includeExceptionDetailInFaults="false" />
            </behavior>
          </serviceBehaviors>
          <endpointBehaviors>
            <behavior name="webbev">
              <webHttp />
            </behavior>
          </endpointBehaviors>
        </behaviors>
        <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
      </system.serviceModel>
    

    Here is the simplified configuration since WCF has the new feature in net4.5

      <system.serviceModel>
        <behaviors>
          <serviceBehaviors>
            <behavior>
              <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true" />
              <serviceDebug includeExceptionDetailInFaults="false" />
            </behavior>
          </serviceBehaviors>
          <endpointBehaviors>
            <behavior>
              <webHttp />
            </behavior>
          </endpointBehaviors>
        </behaviors>
        <bindings>
          <webHttpBinding>
            <binding name="mybinding">
              <security mode="Transport">
                <transport clientCredentialType="None"></transport>
              </security>
            </binding>
          </webHttpBinding>
        </bindings>
        <protocolMapping>
          <add binding="webHttpBinding" scheme="http"/>
          <add binding="webHttpBinding" scheme="https" bindingConfiguration="mybinding"/>
        </protocolMapping>
        <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
      </system.serviceModel>
    

    Then add https address and specify the certificate. enter image description here
    Here is an official document, wish it is useful to you.
    https://learn.microsoft.com/en-us/dotnet/framework/wcf/feature-details/how-to-configure-an-iis-hosted-wcf-service-with-ssl

    Result. enter image description here
    Feel free to let me know if there is anything I can help with.