我試圖使用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。你用