启用 HTTPS,IIS 托管的 WCF 服务,如何保护它

本文关键字:何保护 保护 WCF IIS HTTPS 启用 服务 | 更新日期: 2023-09-27 17:56:44

我构建了一个相当简单的 WCF 服务,它承载在 IIS 7.5 实例上。我已经介绍了保护SSL证书以启用https的必要步骤。我已经解决了所有各种 DNS 设置,所以我现在可以在全世界给定的 Https://URL 上点击我的 WCF。目标是:对将向服务发送数据的大约 5 个客户端进行某种客户端/服务器身份验证。保护此服务的最佳方法是什么? 在这一点上,只有一种方法非常简单。我相信 web.config 和代码隐藏会有一些变化。例子非常感谢。

这是网络.config

<!-- language: lang-xml -->
    <?xml version="1.0"?>
<configuration>
  <appSettings>
    <add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />
  </appSettings>
  <system.web>
    <compilation debug="true" targetFramework="4.5" />
    <httpRuntime targetFramework="4.5"/>
  </system.web>
  <system.serviceModel>
  <services>
    <service name="wcflistener.Service1">
      <endpoint address=""           
      binding="basicHttpBinding"
      bindingConfiguration="secureHttpBinding"
      contract="wcflistener.IService1"/>
      <endpoint address="mex"
      binding="mexHttpsBinding"
      contract="IMetadataExchange" />
    </service>
  </services>
  <bindings>
    <basicHttpBinding>
      <binding name="secureHttpBinding">
        <security mode="Transport">
          <transport clientCredentialType="None"/>
        </security>
      </binding>
    </basicHttpBinding>
  </bindings>
    <behaviors>
      <serviceBehaviors>
        <behavior>
          <!-- To avoid disclosing metadata information, set the values below to false before deployment -->
          <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/>
          <!-- To receive exception details in faults for debugging purposes, set the value below to true.  Set to false before deployment to avoid disclosing exception information -->
          <serviceDebug includeExceptionDetailInFaults="true"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>
  </system.serviceModel>
  <system.webServer>
    <modules runAllManagedModulesForAllRequests="true"/>
    <!--
    To browse web app root directory during debugging, set the value below to true.
    Set to false before deployment to avoid disclosing web app folder information.
    -->
    <directoryBrowse enabled="true"/>
  </system.webServer>
</configuration>

还有非常简单的Service1.svc.cs

 [DataContract]
public class Service1 : IService1
{

    public void SampleMethod(DataTable table, string name)
    {
        //sample method logic here
    } 
}

启用 HTTPS,IIS 托管的 WCF 服务,如何保护它

使用 WCF,可以使用多个安全选项,所有选项都有其优点/缺点:

  1. SSL + 使用 Windows 身份验证。(这对于互联网托管服务来说通常很困难,因为每个人都需要与同一个域控制器通信)
  2. SSL + 用户名/密码:WCF 可以轻松实现此目的,其中客户端传入用户名/密码,服务可以使用预配置的值验证值,并进一步允许客户端。
  3. 基于证书的
  4. 身份验证:通常,可以为客户端提供服务器证书的公钥,以便它们可以使用该公钥调用服务。 但是,这并不能完全识别客户端。 任何人都可以获取您的公钥。
  5. 相互证书或 2 路 SSL:这是当客户端拥有私钥并将公钥提供给服务时。 反之亦然,即服务将其公钥提供给客户端。

这取决于您需要的身份验证级别。 对于极少数客户端,用户名/密码就足够了。(总是有失去这些的风险)

对于主要客户来说,2 路 SSL 非常安全,因为人们不会轻易丢失私钥。

根据您的选择,可以共享更多代码示例。

对于选项 #,请点击此链接。(您可以在以下步骤中使用您的SSL证书)

http://codebetter.com/petervanooijen/2010/03/22/a-simple-wcf-service-with-username-password-authentication-the-things-they-don-t-tell-you/