2010-12-22 102 views
3

我已經創建了一個服務,我需要客戶端傳遞憑據(用戶名和密碼)。此行爲需要X509證書,所以我開始使用makecert.exe進行自簽名開發問題。WCF與WSHttpBinding,消息安全,clientCredentialType =「用戶名」證書SelfHosted問題

因爲我很新手憑證,我看到這個證書是在IIS服務器證書部分創建的,我需要我的服務以後在Windows服務上自我託管,用於測試目的我使用控制檯主機應用程序和一個簡單的Winform應用客戶端。

所以我的問題是,我該如何部署此證書?我不想在任何情況下使用IIS,我可以嵌入證書,我注意到我可以導出爲控制檯/ windows服務主機內的.pfx文件?如何?

我發佈了我的服務和客戶端配置文件,以幫助理解我需要什麼。

服務器配置:

<?xml version="1.0" encoding="utf-8" ?> 
<configuration> 
    <system.serviceModel> 
    <services> 
     <service name="B2B.WCF.Service.B2BService" behaviorConfiguration="wsBehavior"> 
     <endpoint name="WSHttpEndpointB2B" 
        bindingConfiguration="WSBinding" 
        address ="http://localhost:8768/ServB2B" 
        binding="wsHttpBinding" 
        contract="B2B.WCF.Contracts.IB2BContracts"> 
     </endpoint> 
     </service> 
    </services> 
    <behaviors> 
     <serviceBehaviors> 
     <behavior name="wsBehavior"> 
      <serviceMetadata httpsGetEnabled="false"/> 
      <serviceDebug includeExceptionDetailInFaults="true" /> 
      <serviceCredentials> 
      <serviceCertificate findValue="MyServerCert" x509FindType="FindBySubjectName" 
           storeLocation="LocalMachine" storeName="My" /> 
      <userNameAuthentication userNamePasswordValidationMode="Custom" 
            customUserNamePasswordValidatorType="B2B.WCF.Service.UserValidator, B2B.WCF.Service" /> 
      </serviceCredentials> 
     </behavior> 
     </serviceBehaviors> 
    </behaviors> 
    <bindings> 
     <wsHttpBinding> 
     <binding name="WSBinding"> 
      <security mode="Message"> 
      <message clientCredentialType="UserName" /> 
      </security> 
     </binding> 
     </wsHttpBinding> 
    </bindings> 
    </system.serviceModel> 
</configuration> 

客戶端配置:

<?xml version="1.0" encoding="utf-8" ?> 
<configuration> 
    <system.serviceModel> 
    <client> 
     <endpoint name="WSHttpEndpointB2B" 
       bindingConfiguration="WSBinding" behaviorConfiguration="wsBehavior" 
       address ="http://localhost:8768/ServB2B" 
       binding="wsHttpBinding" 
       contract="B2B.WCF.Contracts.IB2BContracts"> 
     <identity> 
      <dns value="MyServerCert"/> 
     </identity> 
     </endpoint> 
    </client> 
    <behaviors> 
     <endpointBehaviors> 
     <behavior name="wsBehavior"> 
      <clientCredentials> 
      <clientCertificate findValue="MyServerCert" x509FindType="FindBySubjectName" 
           storeLocation="LocalMachine" storeName="My"/> 
      <serviceCertificate> 
       <authentication certificateValidationMode="None"/> 
      </serviceCertificate> 
      </clientCredentials> 
     </behavior> 
     </endpointBehaviors> 
    </behaviors> 
    <bindings> 
     <wsHttpBinding> 
     <binding name="WSBinding"> 
      <security mode="Message"> 
      <message clientCredentialType="UserName" /> 
      </security> 
     </binding> 
     </wsHttpBinding> 
    </bindings> 
    </system.serviceModel> 
</configuration> 

Thanx提前

回答

3

你的證書需要導入到Windows證書存儲在承載您的網站上機服務(即「服務器」)和(可選)在使用Web服務的計算機上(即「客戶機」,如果它是不同的機器)。

您應該使用Microsoft管理控制檯(MMC)來執行此操作。首先,您應該根據this文章進行設置。然後根據文章this中的步驟導入您的證書。確保您爲客戶端證書(即「個人」)和根證書(即「受信任的根證書頒發機構」)選擇了正確的存儲。

除非您的Web服務找到您的配置文件中引用的正確證書,否則它將無法啓動。在您的情況下,這是您要存儲在「個人」商店中的"MyServerCert"證書。

+0

是否可以在代碼中安裝證書? – 2010-12-22 17:44:15