【问题标题】:How to get wcf communiction ONLY over Https如何仅通过 Https 获得 wcf 通信
【发布时间】:2013-12-16 00:32:58
【问题描述】:

我想要一个 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 端口访问。

【问题讨论】:

  • 我相信你需要&lt;securityMode="TransportWithMessageCredential"&gt; 来强制只使用 SSL。

标签: c# wcf web-services https wsdl


【解决方案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 的默认值(安全模式的默认值为“消息”)。您可以通过endpoint 元素上的bindingConfiguration 属性分配上面的绑定配置:

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

【讨论】:

  • 谢谢,我忘了添加 bindingConfiguration。有时你只需要第二双眼睛。还像你说的那样添加了TransportWithMessageCredential 模式。完美运行!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-06-22
  • 2012-05-27
  • 1970-01-01
相关资源
最近更新 更多