简体   繁体   English

WCF RESTful HTTPS支持

[英]WCF RESTful HTTPS support

I have [WebInvoke] method which use for ajax call to get data, but in some case ajax call should happens over HTTPS protocol. 我有[WebInvoke]方法,该方法用于ajax调用以获取数据,但是在某些情况下ajax调用应通过HTTPS协议进行。 How to configure my method to work with both HTTP and HTTPS connection 如何配置我的方法以同时使用HTTP和HTTPS连接

this is may method 这是可能的方法

    [ServiceContract(Namespace = "")]
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
    public class ChartService
    {           
        [OperationContract]
        [WebInvoke(Method = "POST", ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Wrapped,
            RequestFormat = WebMessageFormat.Json )]
        public GetDataRes GetData(GetDataReq req)
        { 
            res=DB.GetRes(req);
            return res;
        }
    }

this is web.config 这是web.config

  <system.serviceModel>
      <serviceHostingEnvironment aspNetCompatibilityEnabled="true" />
        <behaviors>
          <endpointBehaviors>
            <behavior name="Won.ICom.Code.Services.ChartServiceAspNetAjaxBehavior">
              <webHttp />
            </behavior>
          </endpointBehaviors>
        </behaviors>
        <services>
          <service name="Won.ICom.Code.Services.ChartService">
            <endpoint address="http://localhost:12345/services/ChartService.svc" behaviorConfiguration="Won.ICom.Code.Services.ChartServiceAspNetAjaxBehavior"
             binding="webHttpBinding" contract="Won.ICom.Code.Services.ChartService" />
          </service>
        </services>
    </system.serviceModel>

Create second End point Change mexHttpBinding to mexHttpsBinding in the second end point. 创建第二个端点在第二个端点中将mexHttpBinding更改为mexHttpsBinding。 In serviceMetadata, we also need to change httpGetEnabled to httpsGetEnabled. 在serviceMetadata中,我们还需要将httpGetEnabled更改为httpsGetEnabled。

<endpoint address="http://localhost:12345/services/ChartService.svc"      bindingConfiguration="TransportSecurity" binding="wsHttpBinding" contract="Won.ICom.Code.Services.ChartService" />
<endpoint address="mex" binding="mexHttpsBinding" contract="IMetadataExchange"/>
</service>

<serviceBehaviors>
<serviceMetadata httpsGetEnabled="true"/>
</serviceBehaviors>
<bindings>
<webHttpBinding>
<binding name="TransportSecurity">
<security mode="Transport" />
</binding>
</webHttpBinding>
</bindings>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM