.net - 使用客户端证书身份验证创建 .NET Web 服务

标签 .net web-services security certificate

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

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

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

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

还是另一种方式?

最佳答案

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

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

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

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

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

为了限制对特定证书的访问,您可以使用 bindingConfiguration 将 WCF 配置文件设置为:
<basicHttpBinding>
  <binding name="MyBasicHttpBinding">
    <security mode="Transport">
      <transport clientCredentialType="Certificate" />
    </security>
  </binding>
</basicHttpBinding>

以及带有自定义证书验证器的 behaviorConfiguration 为:
<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();
    }
}

关于.net - 使用客户端证书身份验证创建 .NET Web 服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6544653/

相关文章:

.net - 有什么方法可以在我的 ASP.NET MVC Web 应用程序中模拟 Claims Principal 吗?

c# - 如何获取数组中字典的键和值?

java - 如何创建实现 @WebService 的 @Singleton bean

git - 将部署(只读)github key 存储在私有(private)仓库中是否安全

.net - 如何检测何时按下热键(快捷键)

.net - EF4 可以使用自定义约定从数据库生成模型

web-services - Fedex SmartPost 开发者账户的 HubId 无效

iphone 网络服务应用程序

asp.net-mvc - asp .net mvc 中的 http 摘要身份验证

security - 无休止的 session : any security risk?