【问题标题】:Creating .NET web service with client certificate authentication使用客户端证书身份验证创建 .NET Web 服务
【发布时间】:2011-09-26 13:02:18
【问题描述】:

我想将对我的 .NET Web 服务的访问限制为特定的客户端列表。他们会将他们的客户证书附加到他们的每个请求中,并且只有在他们“在列表中”时才会得到正确的响应。

但是如何以及在哪里实现这一点的最佳方式是什么?

在 IIS (7.0) 上,我可以设置需要客户端证书选项,但我在哪里可以指定允许访问的客户端证书?我是否需要 Web 服务器计算机的证书存储中的客户端证书的公共部分?

或者必须在代码中处理这样的设置,我以某种方式提取客户端证书 ID 并将其与本地列表匹配?

或者其他方式?

【问题讨论】:

    标签: .net web-services security certificate


    【解决方案1】:

    根据您的安全要求在 IIS7 上创建 WCF 服务的一种方法如下。

    为了托管 WCF 服务,您可能需要在 Web 服务器上运行以下命令:

    "%windir%\Microsoft.NET\Framework\v3.0\Windows Communication Foundation\ServiceModelReg.exe" -r –y
    

    在 IIS 上,您使用 https 绑定(仅)设置您的站点,在 SSL 设置下,您将其设置为需要 SSL 并需要客户端证书。

    仅此一项将仅允许使用有效的客户端证书和 Web 服务器信任的颁发者访问您的服务(和 wsdl)。

    为了限制对特定证书的访问,您可以使用 bindingConfiguration 将 WCF 配置文件设置为:

    <basicHttpBinding>
      <binding name="MyBasicHttpBinding">
        <security mode="Transport">
          <transport clientCredentialType="Certificate" />
        </security>
      </binding>
    </basicHttpBinding>
    

    还有一个带有自定义证书验证器的行为配置:

    <behaviors>
      <serviceBehaviors>
        <behavior name="MyServiceBehavior">
          <serviceMetadata httpGetEnabled="false" httpsGetEnabled="true" />
          <serviceDebug includeExceptionDetailInFaults="false" />
          <serviceCredentials>
            <clientCertificate>
              <authentication certificateValidationMode="Custom"
                customCertificateValidatorType="<project-namespace>.ClientCertificateValidator, <project-namespace>"/>
            </clientCertificate>
          </serviceCredentials>
        </behavior>
      </serviceBehaviors>
    </behaviors>
    

    最后在项目的新类中实现自定义验证器:

    public class ClientCertificateValidator : X509CertificateValidator
    {
        public override void Validate(X509Certificate2 certificate)
        {
          if (certificate.Thumbprint != <allowed-thumbprint>)
            throw new Exception();
        }
    }
    

    【讨论】:

      【解决方案2】:

      如果这些客户端证书源自特定的根 CA,您可以适当地使用 CTL

      否则我认为你有这些选择(取决于你的需要):

      • 如果您有权访问客户端证书 .cer 文件,则可以使用 client certificate mapping 并映射到 Windows 帐户,可选择将它们分组到 AD/本地组和相应的权限中。
      • 提取代码中的 UPN(可能在 HTTP 处理程序中)并根据已授予的 UPN 列表进行验证

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-04-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多