使用SSL

时间:2016-10-25 17:56:04

标签: asp.net wcf ssl

我使用asp.net vb创建了一个REST api,我试图通过安全连接(https)调用api,但是我遇到了错误

The resource cannot be found

我可以使用(http)调用任何方法,但使用(https)我不能。我可以使用(https)访问api(service.svc)的主页但功能问题!!下面是我的配置和功能标题。

    <system.serviceModel>


   <services>
  <service name="RESTAPI" behaviorConfiguration="MyServiceTypeBehaviors">

    <endpoint address="customBinding" binding="customBinding" bindingConfiguration="basicConfig" contract="RESTAPI"/>

<endpoint address="" behaviorConfiguration="HerbalAPIAspNetAjaxBehavior"
      binding="webHttpBinding" contract="HerbalAPI"  />

    <endpoint contract="RESTAPI" binding="mexHttpBinding" address="mex" />


     </service>

</services>

  <!-- **** Services ****-->


<behaviors>

  <serviceBehaviors>
    <behavior name="MyServiceTypeBehaviors">
      <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/>
      <serviceDebug includeExceptionDetailInFaults="true"/>
    </behavior>
  </serviceBehaviors>


  <endpointBehaviors>
    <behavior name="HerbalAPIAspNetAjaxBehavior">
      <webHttp helpEnabled="true" />
    </behavior>
  </endpointBehaviors>

</behaviors>
 <bindings>
  <customBinding>
    <binding name="basicConfig">
      <binaryMessageEncoding/>
      <httpTransport transferMode="Streamed" maxReceivedMessageSize="67108864"/>
    </binding>
  </customBinding>

</bindings>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true"
  multipleSiteBindingsEnabled="true" />

API类

<ServiceContract(Namespace:="")>
<AspNetCompatibilityRequirements(RequirementsMode:=AspNetCompatibilityRequirementsMode.Allowed)>
Public Class RESTAPI
 <OperationContract()>
<WebInvoke(Method:="GET", ResponseFormat:=WebMessageFormat.Json, RequestFormat:=WebMessageFormat.Json)>
Public Function test(ByVal st As String) As JSONResultString
//any code
End Function
End Class

1 个答案:

答案 0 :(得分:7)

您需要在web.config文件中定义特殊绑定配置,以允许SVC服务正确绑定HTTPS请求。

请查看此博文:https://weblogs.asp.net/srkirkland/wcf-bindings-needed-for-https

您的服务已在web.config中定义,只需添加bindingConfiguration属性:

<services>
  <service name="TestService">
    <endpoint address="" behaviorConfiguration="TestServiceAspNetAjaxBehavior"
      binding="webHttpBinding" bindingConfiguration="webBindingHttps" contract="TestService" />
  </service>
</services>

然后定义webHttpBinding的特殊绑定设置,修复HTTPS请求的神奇部分是<security mode="Transport" />

<bindings>
  <webHttpBinding>
    <binding name="webBindingHttps">
     <security mode="Transport">
     </security>
    </binding>
  </webHttpBinding>
</bindings>

这将有效地将服务切换到HTTPS,但如果您希望同时使用HTTP和HTTPS,则需要定义2个绑定配置,然后每个服务有2个相同的端点,其中一个使用http {{1另一个使用https bindingConfiguration,如下所示:

bindingConfiguration