https站点调用wcf的问题解决

这个问题弄了我2天时间。各种纠结最后终于成功。特此记录下

开发环境:vs2010

客户端:silverlight使用自签名证书的https

服务端:wcf

我的需求是客户端地址必须为https方式,服务端不限。

最终成功解决的方式如下:

1.WCF配置为可接受http 和 https请求,代码未改动,配置文件如下:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <system.web>
    <compilation debug="true" targetFramework="4.0">
      <assemblies>
        <add assembly="System.Data.Entity, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
      </assemblies>
    </compilation>
    <httpRuntime executionTimeout ="120"/>
  </system.web>
  <system.serviceModel>
    <bindings>
      <basicHttpBinding>
        <binding name="BBB" maxBufferSize="2147483647" maxReceivedMessageSize="2147483647" receiveTimeout="01:00:00">
          <!--name=随意命名,但要与上面的bindingConfiguration="BBB"对应 -->
          <readerQuotas maxStringContentLength="2147483647" maxArrayLength="2147483647" />
          <security mode="None">
</security>
        </binding>
      </basicHttpBinding>
	<webHttpBinding>
    <binding name="HttpsBinding">
      <security mode="Transport">
        <transport clientCredentialType="None"></transport>
      </security>
    </binding>
  </webHttpBinding>
    </bindings>
    <behaviors>
      <serviceBehaviors>
        <behavior>
          <!-- 为避免泄漏元数据信息,请在部署前将以下值设置为 false 并删除上面的元数据终结点 -->
          <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/>
          <!-- 要接收故障异常详细信息以进行调试,请将以下值设置为 true。在部署前设置为 false 以避免泄漏异常信息 -->
          <serviceDebug includeExceptionDetailInFaults="false" />
        </behavior>
        <behavior name="A">
          <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/>
          <serviceDebug includeExceptionDetailInFaults="true" />
          <dataContractSerializer maxItemsInObjectGraph="2147483647" />
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <protocolMapping>
        <add binding="basicHttpsBinding" scheme="https" />
    </protocolMapping> 
    <services>
      <!--注1:此处的behaviorConfiguration值要跟上面的name值对应-->
      <!--注2:此处的name值不能随便修改,命名格式为:完全命名空间+类名 -->
      <service behaviorConfiguration="A" name="KAM3000_A.WCF.KAMService">
        <!--注1:此处的contract值不能随便修改,命名格式为:完全命名空间+类名 -->
        <!--注2:此处的bindingConfiguration值要与下面 binding name中的name值对应-->
        <endpoint address="" bindingConfiguration="BBB" binding="basicHttpBinding" contract="KAM3000_A.WCF.IKAMService" />
	<endpoint address="" binding="webHttpBinding" bindingConfiguration="HttpsBinding" contract="KAM3000_A.WCF.IKAMService" />
      </service>
    </services>
    <serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
  </system.serviceModel>
  <system.webServer>
    <directoryBrowse enabled="true" />
    <modules runAllManagedModulesForAllRequests="true" />
  </system.webServer>
</configuration>

重点在要有两套endpoint,一个支持http 一个支持 https

两套 Httpbinding  对应一个支持http 一个支持 https

还有httpsGetEnabled="true"

最终在浏览器访问时都能成功访问到。但实际使用时我调用的还是http方式

2.将客户端绑定多一个https方式,证书使用自签名证书

此时客户端用https访问http的wcf时,出现“跨域错误”
3.在IIS根目录C:\inetpub\wwwroot下放入crossdomain.xm和clientaccesspolicy.xml文件,即可正常使用
附上这两个XML内容clientaccesspolicy.xml

<?xml version="1.0" encoding="utf-8"?>
<access-policy>
  <cross-domain-access>
    <policy>
      <allow-from http-request-headers="*">
        <domain uri="*"/>
      </allow-from>
      <grant-to>
        <resource path="/" include-subpaths="true"/>
      </grant-to>
    </policy>
  </cross-domain-access>
</access-policy>

crossdomain.xml

<?xml version="1.0"?>
<!DOCTYPE cross-domain-policy SYSTEM>
<cross-domain-policy>
<allow-http-request-headers-from domain="*" headers="*"/>
</cross-domain-policy>


  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值