2010-06-16 183 views
6

有自 - 承載的WCF服務器(非IIS),並且使用命令行一樣自我 - 託管WCF服務器和SSL

makecert.exe -sr CurrentUser -ss My -a sha1 -n CN=SecureClient -sky exchange -pe 
makecert.exe -sr CurrentUser -ss My -a sha1 -n CN=SecureServer -sky exchange -pe 

這些證書添加到服務器生成證書(在Win XP的)代碼是這樣的:

serviceCred.ServiceCertificate.SetCertificate(StoreLocation.LocalMachine, 
          StoreName.My, X509FindType.FindBySubjectName, "SecureServer"); 



serviceCred.ClientCertificate.SetCertificate(StoreLocation.LocalMachine, 
          StoreName.My, X509FindType.FindBySubjectName, "SecureClient"); 

經過前面的所有操作,我創建了簡單的客戶端來檢查到服務器的SSL連接。

客戶端配置:

<?xml version="1.0" encoding="utf-8" ?> 
<configuration> 
    <system.serviceModel> 
     <bindings> 
      <basicHttpBinding> 
       <binding name="BasicHttpBinding_IAdminContract" closeTimeout="00:01:00" 
        openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00" 
        allowCookies="false" bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard" 
        maxBufferSize="65536" maxBufferPoolSize="524288" maxReceivedMessageSize="65536" 
        messageEncoding="Text" textEncoding="utf-8" transferMode="Buffered" 
        useDefaultWebProxy="true"> 
        <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384" 
         maxBytesPerRead="4096" maxNameTableCharCount="16384" /> 
        <security mode="TransportCredentialOnly"> 
         <transport clientCredentialType="Basic"/> 
        </security> 
       </binding> 
      </basicHttpBinding> 
     </bindings> 
     <client> 
      <endpoint address="https://myhost:8002/Admin" binding="basicHttpBinding" 
       bindingConfiguration="BasicHttpBinding_IAdminContract" contract="Admin.IAdminContract" 
       name="BasicHttpBinding_IAdminContract" /> 
     </client> 
    </system.serviceModel> 
</configuration> 

代碼:

Admin.AdminContractClient client = new AdminContractClient("BasicHttpBinding_IAdminContract"); 
      client.ClientCredentials.UserName.UserName = "user"; 
      client.ClientCredentials.UserName.Password = "pass"; 
      var result = client.ExecuteMethod() 

在執行過程中會收到一個錯誤:

The provided URI scheme 'https' is invalid; expected 'http'.\r\nParameter name: via 

問:如何啓用SSL進行自託管的服務器在哪裏我應該爲客戶端和服務器設置證書嗎? 謝謝。

回答

7

嘗試改變

<security mode="TransportCredentialOnly"> 

<security mode="Transport"> 

,讓我們知道如果讓任何改進。這應該使您的客戶端允許HTTPS連接。

+0

收到此更改後,我收到: 將HTTP請求發送到https:// myhost:8002/Admin時發生錯誤。這可能是由於在HTTPS情況下服務器證書未使用HTTP.SYS正確配置。這也可能是由於客戶端和服務器之間的安全綁定不匹配造成的。 – jitm 2010-06-16 11:16:08

+0

你可以用你的服務器的servicemodel config更新帖子 – 2010-06-16 11:37:26

+0

我一定會看看[WCF Codeplex上的WCF安全指南](http://wcfsecurityguidance.codeplex.com/)。特別是,[本頁談到證書認證](http://wcfsecurity.codeplex.com/wikipage?title=How%20To%20-%20Use%20Certificate%20Authentication%20and%20Message%20Security%20in%20WCF%20calling %20from%20Windows%20Forms&referringTitle =如何%20Tos)。 – 2010-06-16 11:44:37