【问题标题】:WCF service with SSL使用 SSL 的 WCF 服务
【发布时间】:2011-12-19 07:16:04
【问题描述】:

我知道 SSL 证书用于应用程序的安全目的,因此数据传输应该采用加密形式。据我所知,我们必须在主机服务器中为我们的应用程序安装 SSL 证书。

这些天我在 WCF 服务中工作。客户希望我们使用 SSL 证书制作 WCF 服务。

我想知道的是,在 SSL 证书的代码级别是否需要做任何事情。我将在 IIS 中托管我的服务。

使用 SSL 证书配置 WCF 服务的步骤是什么?

我知道知之甚少总是危险的:(

请详细说明。

提前致谢。

【问题讨论】:

  • 您想只保护通道还是使用 SSL 执行客户端身份验证?
  • 你们都使用 SSL 进行客户端身份验证的安全通道..
  • 如果您希望通过 SSL 进行客户端身份验证,是针对单个客户端还是可能有不同的客户端访问此服务?
  • ya 客户端可以变化到 10,000...
  • 在这种情况下,如果您想通过证书区分每个客户端,您可能需要自定义证书验证器。实现自定义证书验证器很简单。

标签: wcf ssl


【解决方案1】:

为了为您的服务配置 2 路 SSL,请执行以下步骤:

  1. 创建一个映射了 https 绑定的网站。
  2. 当 https 绑定映射到网站时,它会要求提供服务器 SSL 证书,用于保护您的传输通道。
  3. 创建一个您希望部署服务的虚拟目录。
  4. 现在正在构建的 WCF 服务需要具有指定服务使用 https 并且客户端使用证书进行身份验证的配置。
  5. 在虚拟目录的 SSL 设置中将选项设置为“接受”,这表明客户端可能通过证书。如果您将其设置为要求,则客户端需要通过证书。

注意:使用证书时,您需要确定哪个证书需要安装在哪个证书存储中。自签名证书可能存在一些例外情况,但可以使用以下代码在客户端绕过它们:

ServicePointManager.ServerCertificateValidationCallback = (sender, cert, chain, error) => true;

一些关于如何实现和使用自定义证书验证器的代码:

public class CustomX509CertificateValidator : System.IdentityModel.Selectors.X509CertificateValidator
    {
        // This Validation function accepts any X.509 Certificate that is self-issued. As anyone can construct such
        // a certificate this custom validator is less secure than the default behavior provided by the
        // ChainTrust X509CertificateValidationMode. The security implications of this should be carefully 
        // considered before using this validation logic in production code. 
        public override void Validate(X509Certificate2 certificate)
        {
            // Check that we have been passed a certificate
            if (certificate == null)
                throw new ArgumentNullException("certificate");

            // Only accept self-issued certificates
            if (certificate.Subject != certificate.Issuer)
                throw new SecurityTokenException("Certificate is not self-issued");
        }
    }

现在在您的 WCF 服务配置文件中使用自定义证书验证器如下所示:

<behaviors>
      <serviceBehaviors>
        <behavior name="CalculatorServiceBehavior">
          <serviceDebug includeExceptionDetailInFaults="true"/>
          <serviceCredentials>
            <!-- 
            The serviceCredentials behavior allows one to specify authentication constraints on client certificates.
            -->
            <clientCertificate>
              <!-- 
              Setting the certificateValidationMode to Custom means that if the custom X509CertificateValidator
              does NOT throw an exception, then the provided certificate will be trusted without performing any
              validation beyond that performed by the custom validator. The security implications of this 
              setting should be carefully considered before using Custom in production code. 
              -->
              <authentication certificateValidationMode="Custom" customCertificateValidatorType="X509CertificateValidator.CustomX509CertificateValidator, service"/>
            </clientCertificate>
            <!-- 
            The serviceCredentials behavior allows one to define a service certificate.
            A service certificate is used by a client to authenticate the service and provide message protection.
            This configuration references the "localhost" certificate installed during the setup instructions.
            -->
            <serviceCertificate findValue="localhost" storeLocation="LocalMachine" storeName="My" x509FindType="FindBySubjectName"/>
          </serviceCredentials>
        </behavior>
      </serviceBehaviors>
    </behaviors>

【讨论】:

  • 感谢您恢复 RAJEST。我想我正在为我的服务寻找这些功能。谢谢
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-07-08
  • 1970-01-01
  • 1970-01-01
  • 2010-09-30
  • 2011-02-24
  • 2016-07-19
  • 2017-03-07
相关资源
最近更新 更多