以下是使用您的安全要求在IIS7上创建WCF服务的一种方法。
为了托管WCF服务,您可能需要在Web服务器上运行以下命令:
"%windir%\Microsoft.NET\Framework\v3.0\Windows Communication Foundation\ServiceModelReg.exe" -r –y
在IIS上,您可以设置https绑定(仅限),并在SSL设置下将其设置为需要SSL和需要客户端证书。
这样设置只允许使用有效的客户端证书和Web服务器信任的颁发者来访问您的服务(和wsdl)。
为了限制对特定证书的访问,您可以在WCF配置文件中设置bindingConfiguration,如下所示:
<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();
}
}