在 WCF 中,要求客户端证书的安全设置可以通过以下步骤实现:
public static Binding CreateCustomBinding()
{
X509CertificateRecipientServiceElement recipientServiceElement = new X509CertificateRecipientServiceElement();
recipientServiceElement.RequireClientCertificate = true;
CustomBinding customBinding = new CustomBinding();
customBinding.Elements.Add(new TextMessageEncodingBindingElement());
customBinding.Elements.Add(new HttpsTransportBindingElement());
customBinding.Elements.Add(recipientServiceElement);
return customBinding;
}
<bindings>
<customBinding>
<binding name="CustomBinding">
<textMessageEncoding />
<httpsTransport />
<x509CertificateRecipientService requireClientCertificate="true" />
</binding>
</customBinding>
</bindings>
<behaviors>
<serviceBehaviors>
<behavior name="ServiceBehavior">
<serviceCredentials>
<serviceCertificate findValue="MyServiceCertificate" storeLocation="LocalMachine" storeName="My" x509FindType="FindBySubjectName" />
<clientCertificate>
<authentication certificateValidationMode="ChainTrust" revocationMode="NoCheck" />
</clientCertificate>
</serviceCredentials>
<serviceMetadata httpsGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="true" />
</behavior>
</serviceBehaviors>
</behaviors>
<service name="MyService" behaviorConfiguration="ServiceBehavior">
<endpoint address="" binding="customBinding" bindingConfiguration="CustomBinding" contract="IMyService" />
</service>
</services>
通过以上步骤,可以实现 WCF 的安全设置,要求客户端证书。
领取专属 10元无门槛券
手把手带您无忧上云