2017-09-15 120 views
0
基於認證

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

回答

0

https://msdn.microsoft.com/en-us/library/ms731074(v=vs.90).aspx例子是不完整的。 使用https wcf服務需要有效的服務器證書才能工作,在您的情況下,客戶端和服務器證書都是必需的。 這是因爲客戶端和服務器都需要在HTTPS連接中相互信任。

要開始,讀https://docs.microsoft.com/en-us/dotnet/framework/wcf/feature-details/message-security-with-mutual-certificates這是包括指定證書認證服務更完整的示例。

對於通過https託管WCF庫工作,你需要做的,以便執行以下操作:

  1. 配置與X.509證書的端口(已 回答 webHttpBinding with certificate
  2. 從您的服務器,創建您的 服務器完全合格的域名,或者,至少包括你的服務器的完全限定域名的DNS的SubjectAltName的通用名稱的證書請求。 (有不同的方法來做到這一點,你可能已經知道這個 雖然)
  3. 發出證明書,並從你的應用程序的組件文件託管WCF 庫(即[組裝服務器上安裝證書
  4. 抓鬥應用程序ID : 的Guid( 「5870aeed-CACA-4734-8b09-5c0615402bcf」)])抓鬥的證書 指紋通過查看證書屬性。
  5. 作爲管理員,打開CMD 並運行此命令綁定X.509證書所使用的端口 通過您的應用程序在服務器上

    的netsh的http添加的sslcert ipport = 0.0.0.0:443 CERTHASH =的appid = {} certstorename = MY

    的netsh的http添加iplisten ip地址= 0.0.0.0: 443

添加到您的服務器代碼:

myServiceHost.Credentials.ServiceCertificate.SetCertificate(StoreLocation.LocalMachine, StoreName.My, X509FindType.FindBySerialNumber, "<certificate thumbprint>"); 

在你的客戶端代碼,由CERT完全限定域名引用您的服務器地址指定爲證書的證書通用名或主題替代名稱