使用JSONP over SSL的WCF服务

时间:2011-03-04 09:49:54

标签: json ssl wcf jsonp

我们有一个SSL配置的网站,托管WCF服务。服务的绑定具有crossDomainScriptAccessEnabled="true",并且使用JSON序列化通信。

当我们从http请求此服务时,它返回JSONP但是当使用HTTPS请求它时它只返回JSON。我需要以任何一种方式使用JSONP,请帮忙。

当前配置如下:

<webHttpBinding>
        <binding name="webHttpBindingWithJsonP" crossDomainScriptAccessEnabled="true" />
</webHttpBinding>

<behaviors>
            <serviceBehaviors>
                <behavior name="JsonServiceBehaviors">
                    <serviceMetadata httpGetEnabled="true"/>
                    <serviceDebug includeExceptionDetailInFaults="true" />
                </behavior>
            </serviceBehaviors>
            <endpointBehaviors><behavior name="webHttpBehavior">
                <webHttp />
            </behavior></endpointBehaviors>
</behaviors>

<services>
            <service name="Backend.CIService" behaviorConfiguration="JsonServiceBehaviors">
                <endpoint address="" binding="webHttpBinding" 
                          bindingConfiguration="webHttpBindingWithJsonP" contract="Backend.ICIService"
                          behaviorConfiguration="webHttpBehavior"/>
            </service></services>

1 个答案:

答案 0 :(得分:19)

如果您使用此配置会发生什么:

<webHttpBinding>
  <binding name="jsonp" crossDomainScriptAccessEnabled="true" />
  <binding name="jsonpSsl" crossDomainScriptAccessEnabled="true">
    <security mode="Transport" />
  </binding>
</webHttpBinding>

<behaviors>
  <serviceBehaviors>
    <behavior name="JsonServiceBehaviors">
      <serviceDebug includeExceptionDetailInFaults="true" />
    </behavior>
  </serviceBehaviors>
  <endpointBehaviors>
    <behavior name="webHttpBehavior">
      <webHttp />
    </behavior>
  </endpointBehaviors>
</behaviors>

<services>
  <service name="Backend.CIService" behaviorConfiguration="JsonServiceBehaviors">
    <endpoint address="" binding="webHttpBinding" 
      bindingConfiguration="jsonp" contract="Backend.ICIService"
      behaviorConfiguration="webHttpBehavior"/>
    <endpoint address="" binding="webHttpBinding" 
      bindingConfiguration="jsonpSsl" contract="Backend.ICIService"
      behaviorConfiguration="webHttpBehavior"/>
  </service>
</services>

问题在于,如果要通过HTTP和HTTPS呼叫服务,则必须提供两个端点 - 一个用于HTTP,另一个用于HTTPS。