根据您的安全要求在 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();
}
}