如何让WCF服务使用HTTPS协议

How to make WCF Service Use HTTPS protocol

我有一个 WCF 服务,它可以用作休息和肥皂。 我正在尝试通过 HTTPS 协议接收请求,但无论我如何尝试都无法接收。

我试过根据这个post修改web.config文件:

我尝试了其他几种在网上找到的类似方法。 但无论我做什么,我总是得到: "Could not find a base address that matches scheme http for the endpoint with binding WSHttpBinding. Registered base address schemes are [https]." 运行 调试模式下的项目出错。

我的配置如下:

  <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>

我需要做什么才能在 WCF 上启用 HTTPS?

我们应该添加使用传输层安全性的额外服务端点,并在 IIS 站点绑定模块中添加 https 绑定。请看我的配置。

<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>

由于WCF在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>

然后添加https地址并指定证书。
这里有一份官方文档,希望对你有用。
https://docs.microsoft.com/en-us/dotnet/framework/wcf/feature-details/how-to-configure-an-iis-hosted-wcf-service-with-ssl

结果。
如果有什么我可以帮忙的,请随时告诉我。