2012-07-14 200 views
11

問題:WCF上的X.509證書?

我正在一臺機器上開發這個程序。

服務工作在開發服務器罰款,但是當我嘗試在IIS託管服務它給了我一個錯誤:

使用下面的搜索條件找不到X.509證書:STORENAME「我',StoreLocation'CurrentUser',FindType'FindBySubjectName',FindValue'WCFServer'。

那麼無論如何我能解決這個問題嗎? 我從

http://www.codeproject.com/KB/WCF/9StepsWCF.aspx

證書創建

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

證書嘗試這種代碼存在於個人和可信的人在MMC文件夾

服務具有接受頭號功能,返回字符串並正常工作

這是我的服務web.config:

<system.serviceModel> 
    <bindings> 
     <wsHttpBinding> 
     <binding name="wsHttpEndpointBinding"> 
      <security> 
      <message clientCredentialType="Certificate" /> 
      </security> 
     </binding> 
     </wsHttpBinding> 
    </bindings> 
    <services> 
     <service name="WCFServiceCertificate.Service1" behaviorConfiguration="WCFServiceCertificate.Service1Behavior"> 
     <!-- Service Endpoints --> 
     <endpoint address="" binding="wsHttpBinding" bindingConfiguration="wsHttpEndpointBinding" contract="WCFServiceCertificate.IService1"> 
      <!-- 
       Upon deployment, the following identity element should be removed or replaced to reflect the 
       identity under which the deployed service runs. If removed, WCF will infer an appropriate identity 
       automatically. 
      --> 
      <!--<identity> 
      <dns value="localhost"/> 
      </identity>--> 
     </endpoint> 
     <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/> 
     </service> 
    </services> 
    <behaviors> 
     <serviceBehaviors> 
     <behavior name="WCFServiceCertificate.Service1Behavior"> 
      <!-- To avoid disclosing metadata information, set the value below to false and remove the metadata endpoint above before deployment --> 
      <serviceMetadata httpGetEnabled="true"/> 
      <!-- To receive exception details in faults for debugging purposes, set the value below to true. Set to false before deployment to avoid disclosing exception information --> 
      <serviceDebug includeExceptionDetailInFaults="false"/> 
      <serviceCredentials> 
      <clientCertificate> 
       <authentication certificateValidationMode="PeerTrust"/> 
      </clientCertificate> 
      <serviceCertificate findValue="WcfServer" 
           storeLocation="CurrentUser" 
           storeName="My" 
           x509FindType="FindBySubjectName" /> 
      </serviceCredentials> 
     </behavior> 
     </serviceBehaviors> 
    </behaviors> 
    </system.serviceModel> 

這是我的客戶端配置

<system.serviceModel> 
       <bindings> 
    <wsHttpBinding> 
    <binding name="WSHttpBinding_IService1" closeTimeout="00:01:00" 
    openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00" 
    bypassProxyOnLocal="false" transactionFlow="false" hostNameComparisonMode="StrongWildcard" 
    maxBufferPoolSize="524288" maxReceivedMessageSize="65536" messageEncoding="Text" 
    textEncoding="utf-8" useDefaultWebProxy="true" allowCookies="false"> 
    <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384" 
     maxBytesPerRead="4096" maxNameTableCharCount="16384" /> 
    <reliableSession ordered="true" inactivityTimeout="00:10:00" 
     enabled="false" /> 
    <security mode="Message"> 
     <transport clientCredentialType="Windows" proxyCredentialType="None" 
     realm="" /> 
     <message clientCredentialType="Certificate" negotiateServiceCredential="true" 
     algorithmSuite="Default" establishSecurityContext="true" /> 
    </security> 
    </binding> 
    </wsHttpBinding> 
    </bindings> 
       <client> 
    <endpoint address="http://localhost:1387/Service1.svc" behaviorConfiguration="CustomBehavior" 
    binding="wsHttpBinding" bindingConfiguration="WSHttpBinding_IService1" 
    contract="ServiceReference1.IService1" name="WSHttpBinding_IService1"> 
    <identity> 
    <certificate encodedValue="AwAAAAEAAAAUAAAA9YoGKvsMLFkeO1WjaCLReQuz1ysgAAAAAQAAALUBAAAwggGxMIIBX6ADAgECAhDDvb3bnmzhsERpNTWEBYQXMAkGBSsOAwIdBQAwFjEUMBIGA1UEAxMLUm9vdCBBZ2VuY3kwHhcNMTEwMzA0MDcwNzU3WhcNMzkxMjMxMjM1OTU5WjAUMRIwEAYDVQQDEwlXY2ZTZXJ2ZXIwgZ8wDQYJKoZIhvcNAQEBBQADgY0AMIGJAoGBAM9e4DwCDYJ4l6myt1QadHzXoqCH2wa1aUjiab1aK/7d/1LZ00KfPJw8kKB358serjEi9SMg0UeyGtl0+byJ8PqShfv4MUTHZcPaWy99vHaYHwH7T9hVwY5RANBWyFy6nf1rXDh/cB2qm0Q/xN5xElOtheFqUoL8Ua6fcP33BAWPAgMBAAGjSzBJMEcGA1UdAQRAMD6AEBLkCS0GHR1PAI1hIdwWZGOhGDAWMRQwEgYDVQQDEwtSb290IEFnZW5jeYIQBjdsAKoAZIoRz7jUqlw19DAJBgUrDgMCHQUAA0EAKlaHJQNdC9VgPuHlVuniQJd+fHoVOU62nl374iXYdQus5KDgKz9RHWAtjhpToBB4sOXOnwTkJfcyJWBf6J14Mw==" /> 
    </identity> 
    </endpoint> 
    </client> 
       <behaviors> 
         <endpointBehaviors> 
           <behavior name="CustomBehavior"> 
             <clientCredentials> 
               <clientCertificate findValue="WcfClient" 
           x509FindType="FindBySubjectName" 
           storeLocation="CurrentUser" 
           storeName="My"/> 
               <serviceCertificate> 
                 <authentication certificateValidationMode="PeerTrust"/> 
               </serviceCertificate> 
             </clientCredentials> 
           </behavior> 
         </endpointBehaviors> 
       </behaviors> 
     </system.serviceModel> 

,並簡單地調用使用該

Service1Client obj = new Service1Client(); 
      Response.Write(obj.GetData(12)); 

現在,當我運行一切正常,沒有問題,在客戶端的服務。

正如你可以在服務器上運行開發服務器。

但是當我試圖東道國,在IIS它給了我一個錯誤,

不能使用下面的搜索條件找到的X.509證書的服務: STORENAME「我」,StoreLocation「CurrentUser」, FindType 'FindBySubjectName',FindValue'WCFServer'。

那麼無論如何我能解決這個問題嗎?

回答

13

由於x0n提到,Cassini將作爲您當前的用戶運行,但IIS將作爲IUSR運行。從這個導入證書,具有私鑰,到LOCALMACHINE \個人(LOCALMACHINE \我的),並改變你的服務配置:

 <serviceCertificate findValue="WcfServer" 
          storeLocation="CurrentUser" 
          storeName="My" 
          x509FindType="FindBySubjectName" /> 

要這樣:

 <serviceCertificate findValue="WcfServer" 
          storeLocation="LocalMachine" 
          storeName="My" 
          x509FindType="FindBySubjectName" /> 
0

很明顯,在配置的位置找不到證書。確保您使用哪個用戶來運行該服務。也許該服務正在本地系統帳戶或本地服務帳戶或IIS用戶上運行,並且您將該證書作爲其他用戶來安裝?在MMC中添加一個管理單元以查看服務帳戶或您用於該服務的其他帳戶(不是當前用戶)中的證書。

+0

謝謝您的建議。我在服務器上使用,但客戶端得到服務錯誤: – 2012-07-14 11:15:57

+0

未提供客戶端證書。在ClientCredentials中指定客戶端證書。 – 2012-07-14 11:16:17

1

將證書安裝到計算機商店的「我的」中,以便它可供所有用戶使用。您已將它安裝在當前用戶的「我的」中。開發服務器以當前用戶身份運行,所以這就是它的工作原理。

1

你也許必須建立dns值,意思是證書的名稱,在客戶端> endopoint> indentity(如下面的代碼所述)

<client> 
     <endpoint address="http://localhost/FrontPMWebServiceSetup111/FpmService.svc" 
      binding="wsHttpBinding" bindingConfiguration="WSHttpBinding_IService" 
      contract="FPMServiceReference.IService" name="WSHttpBinding_IService"> 
      <identity> 
      <dns value="WCfServer" /> 
      </identity> 
     </endpoint> 
     </client>