zoukankan      html  css  js  c++  java
  • 通过配置web.config使WCF向外提供HTTPS的Restful Service

    如何通过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>
    复制代码

    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/ 

  • 相关阅读:
    Codeforces 1190C Tokitsukaze and Duel game
    2019牛客多校第一场E ABBA 贪心 + DP
    Codeforces 1195E OpenStreetMap 单调队列套单调队列
    由 Vue 中三个常见问题引发的深度思考
    jszip打包上传下载
    Ubuntu切换登录用户和root用户
    vue2.0右键菜单
    main.js中import引入css与引入js的区别
    node和npm版本引起的安装依赖和运行项目失败问题
    reduce()之js与python
  • 原文地址:https://www.cnblogs.com/Alex80/p/9455860.html
Copyright © 2011-2022 走看看