我的代码看起来像这样。为了简洁起见,我删除了一些代码块。 此代码适用于 wsHttp 绑定,但不适用于 netTcp。我是不是错过了什么?
Binding binding = _wcfBindingProvider.GetWcfBinding(bindingName);//read from config
//read cert from store and add. this is successful
ClientCredentials clientCredentials =_clientCredentialsProvider.CreateClientCredentials();
BindingParameterCollection bindingParameters = new BindingParameterCollection();
bindingParameters.Add(clientCredentials);
foreach (var behavior in behaviors)
{
bindingParameters.Add(behavior);
}
IChannelFactory<IRequestChannel> factory = null;
factory = binding.BuildChannelFactory<IRequestChannel>(bindingParameters);
factory.Open(); //ERROR. No certificate present in Client Credentials
使用nettcpbinding时,默认提供的传输级别安全性是windows(需要证书)。因此,您需要在代码中将 Mode 和 ClientCredentialType 设置为 TransportWithMessageCredential 和 Certificate,如下所示:
NetTcpBinding b = new NetTcpBinding(SecurityMode.TransportWithMessageCredential);
b.Security.Message.ClientCredentialType = MessageCredentialType.Certificate;
或者你也可以修改web.config中的代码:
<bindings>
<netTcpBinding>
<binding name="myTcpBinding">
<security mode="TransportWithMessageCredential" >
<message clientCredentialType="Windows" />
</security>
</binding>
</netTcpBinding>
</bindings>
其他设置或更详细的信息可以在本文中找到。希望有帮助。