我试图使用MSDN示例https://msdn.microsoft.com/en-us/library/ms731074(v=vs.90).aspx证书在WCF
这是服务器代码来理解基于证书的验证:
WSHttpBinding binding = new WSHttpBinding();
binding.Security.Mode = SecurityMode.Transport;
binding.Security.Transport.ClientCredentialType = HttpClientCredentialType.Certificate;
// Create the URI for the endpoint.
Uri httpUri = new Uri("https://localhost/Calculator");
// Create the service and add an endpoint.
ServiceHost myServiceHost = new ServiceHost(typeof(ServiceModel.Calculator), httpUri);
myServiceHost.AddServiceEndpoint(typeof(ServiceModel.ICalculator), binding, "");
// Open the service.
myServiceHost.Open();
Console.WriteLine("Listening...");
Console.ReadLine();
// Close the service.
myServiceHost.Close();
这是客户端的代码,我写道:
ChannelFactory<ICalculator> factory = null;
WSHttpBinding binding = new WSHttpBinding();
binding.Security.Mode = SecurityMode.Transport;
binding.Security.Transport.ClientCredentialType = HttpClientCredentialType.Certificate;
EndpointAddress address = new EndpointAddress("https://localhost/Calculator");
factory = new ChannelFactory<ICalculator>(binding, address);
System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12 | SecurityProtocolType.Ssl3;
factory.Credentials.ClientCertificate.SetCertificate(StoreLocation.CurrentUser, StoreName.My, X509FindType.FindBySubjectName, "sroger");
ICalculator channel = factory.CreateChannel();
int y = channel.add(9, 8);
我收到以下例外情况:
类型“System.ServiceModel.CommunicationException”的未处理的异常出现在mscorlib.dll
其他信息:同时使该HTTP请求到https://localhost/Calculator发生错误。这可能是由于在HTTPS情况下服务器证书未使用HTTP.SYS正确配置。这也可能是由于客户端和服务器之间的安全绑定不匹配造成的。
我从同一台机器上运行客户端和服务器。 “sroger”是我当前用户\个人\证书中的证书,它对应于我的机器名称。 不知道该从哪里做什么..任何想法? 在服务器代码中使用什么证书服务器?
谢谢
Gulumal。你用