2017-10-09 76 views
3

我已經從我的客戶端安裝了一個證書到我的PC上,這是訪問他們的Web服務所需要的。但是當我試圖從我的C#windows服務訪問Web服務時,我無法從代碼中找到證書。 這裏是我的代碼:如何使用C#中的客戶端證書調用Web服務?

private X509Certificate findCertificate() 
{ 
     X509Store store = new X509Store(StoreName.Root, StoreLocation.CurrentUser); 
     store.Open(OpenFlags.ReadOnly); 
     string certThumbprint = string.Empty; 

     X509Certificate cert = new X509Certificate(); 
     for (int i = 0; i < store.Certificates.Count; i++) 
     { 
      certThumbprint = store.Certificates[i].Thumbprint.ToString().ToUpper(); 
      if (certThumbprint == "‎176455DB76886FF2BA3C122F8B36322F647CB2FD")//when debugging then debugger is not coming into this line even if it finds the thumbprint 
      { 
       cert = store.Certificates[i]; 
      } 
     } 
     return cert; 
} 

而且,我試圖做同樣的App.config中但我打的錯誤爲:

invalid hexadecimal string format. inner exception null

這裏是我的App.config

<?xml version="1.0" encoding="utf-8"?> 
<configuration> 
<system.serviceModel> 
    <bindings> 
    <customBinding> 
    <binding name="PrivatmoneyPortBinding" > 
     <security defaultAlgorithmSuite="Basic128" authenticationMode="MutualCertificate" 
      requireDerivedKeys="false" includeTimestamp="true" messageProtectionOrder="SignBeforeEncrypt" messageSecurityVersion="WSSecurity10WSTrust13WSSecureConversation13WSSecurityPolicy12BasicSecurityProfile10" 
      requireSignatureConfirmation="false"> 

      <localClientSettings cacheCookies="true" detectReplays="true" 
         replayCacheSize="900000" maxClockSkew="00:05:00" maxCookieCachingTime="00:05:00" 
         replayWindow="00:05:00" sessionKeyRenewalInterval="10:00:00" 
         sessionKeyRolloverInterval="00:05:00" reconnectTransportOnFailure="true" 
         timestampValidityDuration="00:05:00" cookieRenewalThresholdPercentage="60" /> 
        <localServiceSettings detectReplays="true" issuedCookieLifetime="10:00:00" 
         maxStatefulNegotiations="128" replayCacheSize="900000" maxClockSkew="00:05:00" 
         negotiationTimeout="00:01:00" replayWindow="00:05:00" inactivityTimeout="00:02:00" 
         sessionKeyRenewalInterval="15:00:00" sessionKeyRolloverInterval="00:05:00" 
         reconnectTransportOnFailure="true" maxPendingSessions="128" 
         maxCachedCookies="1000" timestampValidityDuration="00:05:00" /> 
     </security> 
       <textMessageEncoding maxReadPoolSize="64" maxWritePoolSize="16" 
        messageVersion="Default" writeEncoding="utf-8"> 
        <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384" 
         maxBytesPerRead="4096" maxNameTableCharCount="16384" /> 
       </textMessageEncoding> 
       <httpsTransport manualAddressing="false" maxBufferPoolSize="524288" 
        maxReceivedMessageSize="65536" allowCookies="false" authenticationScheme="Anonymous" 
        bypassProxyOnLocal="false" decompressionEnabled="true" 
        keepAliveEnabled="true" maxBufferSize="65536" proxyAuthenticationScheme="Anonymous" 
        realm="" transferMode="Buffered" unsafeConnectionNtlmAuthentication="true" 
        useDefaultWebProxy="true" requireClientCertificate="true" /> 
    </binding> 
    </customBinding> 
</bindings> 
<client> 
    <endpoint address="https://pmtest.xxxx.xx:xxxx/xxxxx/xxxxx?wsdl" behaviorConfiguration="NewClientEPBehavior" 
     binding="customBinding" bindingConfiguration="PrivatmoneyPortBinding" 
     contract="PrivatMoney.PrivatmoneyPort" name="PrivatmoneyPort"> 
    </endpoint> 
</client> 
    <behaviors> 
     <endpointBehaviors> 
      <behavior name="NewClientEPBehavior"> 
       <clientCredentials> 
        <serviceCertificate> 
      <authentication certificateValidationMode="PeerTrust" trustedStoreLocation="CurrentUser" /> 
        </serviceCertificate> 
        <clientCertificate storeLocation="CurrentUser" storeName="Root" findValue="‎176455DB76886FF2BA3C122F8B36322F647CB2FD" x509FindType="FindByThumbprint" /> 

     </clientCredentials> 
      </behavior> 
     </endpointBehaviors> 
    </behaviors> 
</system.serviceModel> 
</configuration> 

回答

1

我不知道你是如何得到指紋的。就我而言,我從證書詳細信息中選擇了它(來自mmc的GUI)。問題是我選擇了更多。 在開始時有一些不可見的字符,當它粘貼到配置時不會顯示。

選擇除第一個字符以外的指紋並將其複製到剪貼板。將第一個字符輸入到配置中,並從剪貼板粘貼其餘的字符。

+0

感謝您的回答。我試過了你的建議,但仍然有同樣的錯誤。你能否建議爲什麼它會拋出這個錯誤?或者其他任何方式來檢查證書的存在? – user7336033

+0

刪除包含引號**的指紋**並手動輸入。如果您收到此消息「無效的十六進制字符串格式」,則必須有一些無效的字符。 – pepo

+0

它的工作謝謝你。但是現在遇到了不同的錯誤:私鑰在x 509證書中不存在 – user7336033

相關問題