如何通过Https进行wcf通信

时间:2013-12-16 00:32:58

标签: c# wcf web-services https wsdl

我希望拥有一个WCF服务,所有请求都经过认证,授权并仅通过https发送。 我为SLL生成了一个证书。对于开发我正在使用ISS Express。 同样在web.config中我将任何http选项设置为false。但仍然在生成的WSDL中,当我使用WCFStorm检查服务方法时,它仍使用http://localhost:1947而不是https://localhost:44300我声明。我需要改变什么以确保所有通信都将通过https? 这是我的web.config文件:

<system.serviceModel>
    <bindings>
      <wsHttpBinding>
        <binding name="secureBinding">
          <security mode="Message">
            <message clientCredentialType="UserName" />
          </security>
        </binding>
      </wsHttpBinding>
    </bindings>
    <services>
      <service name="AF.Services.AFService" behaviorConfiguration="AFServiceBehavior">
        <endpoint address="AFService.svc"
                  binding="wsHttpBinding"
                  contract="AF.Common.Services.IAFService" />
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior name="AFServiceBehavior">
          <serviceCredentials>
            <serviceCertificate findValue="AFCert"
              x509FindType="FindBySubjectName" storeLocation="LocalMachine"
              storeName="My" />
            <userNameAuthentication
                userNamePasswordValidationMode="Custom"
                customUserNamePasswordValidatorType="AF.Services.UserValidator, AF.Services" />
          </serviceCredentials>
          <serviceMetadata httpsGetEnabled="true" httpGetEnabled="false" />
          <!-- TODO zmienić przed deployem!!-->
          <serviceDebug includeExceptionDetailInFaults="true" />
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <protocolMapping>
      <add binding="basicHttpsBinding" scheme="https" />
    </protocolMapping>
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
  </system.serviceModel>
  <system.webServer>
    <modules runAllManagedModulesForAllRequests="true" />
    <directoryBrowse enabled="true" />
  </system.webServer>

描述如何使用该服务的网页只能通过44300 https端口访问。

1 个答案:

答案 0 :(得分:1)

在“使用配置”部分中查看How to: Use Transport Security and Message Credentials,您的绑定配置中似乎需要以下安全设置:

<security mode="TransportWithMessageCredential">
  <message clientCredentialType="UserName" />
</security>

所以你的完整绑定配置如下所示:

<wsHttpBinding>
  <binding name="secureBinding">
    <security mode="TransportWithMessageCredential">
      <message clientCredentialType="UserName" />
    </security>
  </binding>
</wsHttpBinding>

另请注意,您实际上并未将定义的绑定分配给端点,因此目前您正在获取wsHttpBinding的默认值(安全模式的默认值为“Message”)。您可以通过bindingConfiguration元素上的endpoint属性分配上面的绑定配置:

<services>
  <service name="AF.Services.AFService" behaviorConfiguration="AFServiceBehavior">
    <endpoint address="AFService.svc"
              binding="wsHttpBinding"
              bindingConfiguration="secureBinding"
              contract="AF.Common.Services.IAFService" />
  </service>
</services>