如何使WCF服务使用HTTPS协议 [英] How to make WCF Service Use HTTPS protocol

查看:120
本文介绍了如何使WCF服务使用HTTPS协议的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

我已经尝试根据这篇文章修改web.config文件:

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

如何在WCF服务中启用HTTPS

我尝试了几种其他在网络上发现的类似方法. 但是,无论我做什么,我总会得到: 找不到与绑定WSHttpBinding的端点的方案http匹配的基地址.注册的基地址方案为[https]." 在调试模式下运行项目时出错.

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.

我的配置如下:

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

What do I need to do enable HTTPS on WCF?

推荐答案

我们应该添加使用传输层安全性的其他服务终结点,并在IIS站点绑定模块中添加https绑定.请查看我的配置.

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>

这是简化的配置,因为WCF在net4.5中具有新功能

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>

然后添加https地址并指定证书.
这是一份正式文件,希望对您有用.
https://docs.microsoft.com/zh-cn/dotnet/framework/wcf/feature-details/how-to-configure-an-iis-hosted-wcf-service-with-ssl

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

结果.
随时让我知道是否有什么可以帮助您的.

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

这篇关于如何使WCF服务使用HTTPS协议的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆