WCF如何使用X509證書(安裝和錯誤)(二)


從當前用戶導出到本地計算機的的證書格式為.pfx 並且要導出私鑰。

 

 

如何創建證書:

      makecert.exe -sr LocalMachine -ss My -a sha1 -n CN=JiangServer -sky exchange -pe     (服務端證書)

      makecert.exe -sr LocalMachine -ss My -a sha1 -n CN=JiangClient -sky exchange -pe      (客戶端證書)

各種參數的介紹

屬性解析

-sr

指定的證書存儲區中的注冊表位置。 
currentUser 
指定注冊版存儲位置為 HKEY_CURRENT_USER. 
localMachine 
指定注冊版存儲位置為 HKEY_LOCAL_MACHINE.

-ss

指定證書存儲的位置。

-a

指定相關的算法,可以選擇 MD5 算法或者 SHA1算法

-n

指定證書的名稱。該名稱遵循X.500命名標准。簡單例子如 "CN=MyName" 格式,如果沒有指定/n開關,證書默認的名稱是"Joe's Software Emporium"。

-sky

證書鍵類型。可以設置為 exchange 或者 signature。

-pe

證書可導出

詳細說明:見msdn。

   證書創建成功后!—

 

image

 

 

服務端config配置非常重要,如下:

復制代碼
<?xml version="1.0" encoding="utf-8"?>
<configuration>

<system.web>
<compilation debug="true" targetFramework="4.0" />
</system.web>
<system.serviceModel>
<services>
<service name="WcfService.Service1" behaviorConfiguration="CustomBehavior">

<endpoint
binding="mexHttpBinding"
contract
="IMetadataExchange"
address
="mex" />
<endpoint address="" binding="wsHttpBinding" contract="WcfService.IService1" bindingConfiguration="CustomBinding"/>

</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="CustomBehavior">
<!-- 為避免泄漏元數據信息,請在部署前將以下值設置為 false 並刪除上面的元數據終結點 -->
<serviceMetadata httpGetEnabled="true"/>
<!-- 要接收故障異常詳細信息以進行調試,請將以下值設置為 true。在部署前設置為 false 以避免泄漏異常信息 -->
<serviceDebug includeExceptionDetailInFaults="false"/>

<serviceCredentials>
<!-- 服務端采用證書詳細配置 findValue :創建證書名稱 storeName:證書儲存詳細位於哪 storeLocation :證書儲存位於當前本機用戶 X509FindType : x509查找證書主題名-->
<serviceCertificate findValue="JiangServer" storeName="My" storeLocation="LocalMachine" x509FindType="FindBySubjectName"/>
<!--客戶端驗證方式-->
<clientCertificate>
<authentication certificateValidationMode="None"/>
</clientCertificate>
</serviceCredentials>

</behavior>
</serviceBehaviors>
</behaviors>
<serviceHostingEnvironment multipleSiteBindingsEnabled="true" />

<bindings>
<wsHttpBinding>
<binding name="CustomBinding">
<!--驗證方式-->
<security mode="Message">
<message clientCredentialType="Certificate"/>
</security>
</binding>
</wsHttpBinding>
</bindings>

</system.serviceModel>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true"/>
</system.webServer>

</configuration>
復制代碼



 

這樣就基本簡單的x509驗證方式已經配置好了,發布到IIS中。運行如下:

-----------------------------------------------------------------------------------------------------------運行后報錯---------------------------

“/”應用程序中的服務器錯誤。
--------------------------------------------------------------------------------

密鑰集不存在。

說明: 執行當前 Web 請求期間,出現未經處理的異常。請檢查堆棧跟蹤信息,以了解有關該錯誤以及代碼中導致錯誤的出處的詳細信息。 

異常詳細信息: System.Security.Cryptography.CryptographicException: 密鑰集不存在。


源錯誤: 

執行當前 Web 請求期間生成了未經處理的異常。可以使用下面的異常堆棧跟蹤信息確定有關異常原因和發生位置的信息。  

堆棧跟蹤: 


[CryptographicException: 密鑰集不存在。
]
   System.Security.Cryptography.Utils.CreateProvHandle(CspParameters parameters, Boolean randomKeyContainer) +450
   System.Security.Cryptography.Utils.GetKeyPairHelper(CspAlgorithmType keyType, CspParameters parameters, Boolean randomKeyContainer, Int32 dwKeySize, SafeProvHandle& safeProvHandle, SafeKeyHandle& safeKeyHandle) +158
   System.Security.Cryptography.RSACryptoServiceProvider.GetKeyPair() +231
   System.Security.Cryptography.X509Certificates.X509Certificate2.get_PrivateKey() +537
   System.ServiceModel.Security.SecurityUtils.EnsureCertificateCanDoKeyExchange(X509Certificate2 certificate) +78

[ArgumentException: 可能證書“CN=GAServer1”沒有能夠進行密鑰交換的私鑰,或者進程可能沒有訪問私鑰的權限。有關詳細信息,請參見內部異常。]

 

 

image

 

分析錯誤提示,應該是沒有權限呀,我們打開”MMC” 打到我的們 My --證書中的 JiangServer 設置權限。

image

添加Everyone --讀取

image

 

再次運行wcf,成功了!

image

現在服務端已經 Deployment!再創建客戶端,引用wcf服務。引用wcf服務成功后,我要再次要Deployment   “App.config”文件,添加驗證信息。

 

 

復制代碼
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.serviceModel>
<bindings>
<wsHttpBinding>
<binding name="abc" 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">
<message clientCredentialType="Certificate"/>
</security>
</binding>
</wsHttpBinding>
</bindings>
<client>
<endpoint address="http://192.168.1.3/Service1.svc" binding="wsHttpBinding"
bindingConfiguration
="abc" contract="ServiceReference1.IService1"
name
="WSHttpBinding_IService1" behaviorConfiguration="CustomBehavior">
<identity>
<!--成功引用后,自動生成的碼-->
<certificate encodedValue="AwAAAAEAAAAUAAAAIAUN/+3YklX/nz/t50hALxjci4IgAAAAAQAAALcBAAAwggGzMIIBYaADAgECAhBEsG++ZOulskOwScx8Gti4MAkGBSsOAwIdBQAwFjEUMBIGA1UEAxMLUm9vdCBBZ2VuY3kwHhcNMTExMjMwMDI1MjE1WhcNMzkxMjMxMjM1OTU5WjAWMRQwEgYDVQQDEwtKaWFuZ1NlcnZlcjCBnzANBgkqhkiG9w0BAQEFAAOBjQAwgYkCgYEA8HGfOESdAJA6CfUCKxsjVx+G50jzbCykCQT2UzYLhmTZn0/jRt3AhwcJn4wO7tU5xNhZUXhLc/Vxk8apJT6Y7fSv9A02mbX5GShVTuRCpJJZN89VmEKAoWfV1n7iMsbUFBzAQm71+9K3KMAWs77ymYbBb6aVXyxfyYfuPrC/3xsCAwEAAaNLMEkwRwYDVR0BBEAwPoAQEuQJLQYdHU8AjWEh3BZkY6EYMBYxFDASBgNVBAMTC1Jvb3QgQWdlbmN5ghAGN2wAqgBkihHPuNSqXDX0MAkGBSsOAwIdBQADQQBVVrkT8SCHXE3KaXWMX8x5PplYazhf+ibhjkg8P3CjldB9h12BmNKtbo1on7GxrNJb0drOxYB2vqjbolQ82NZT" />
</identity>
</endpoint>
</client>
<!--添加以下配置-->
<behaviors>
<endpointBehaviors>
<behavior name="CustomBehavior">
<clientCredentials>
<!--客戶端證書-->
<clientCertificate findValue="JiangClient" storeName="My" storeLocation="LocalMachine" x509FindType="FindBySubjectName"/>
<serviceCertificate>
<authentication certificateValidationMode="None"/>
</serviceCertificate>
</clientCredentials>
</behavior>
</endpointBehaviors>
</behaviors>
</system.serviceModel>
</configuration>

復制代碼

 

啟動客戶端,調用成功!

image

注意:如果調用wcf失敗,幾個錯誤信息

1>   : 無法使用以下搜索標准找到 X.509 證書: StoreName“My”、StoreLocation“LocalMachine”、FindType“FindBySubjectName”、FindValue“JiangClient1”。

image

解決辦法:導入證書(JiangClient1)或創建此證書,注意報的錯誤信息中的證書所儲存位置

 

2 > : 未提供客戶端證書。請在 ClientCredential 中指定一個客戶端證書。

image

 

解決辦法: 因為服務端使用了證書驗證,所以要在客戶端配置證書。

image

再次調用成功!以上就是我在設置X509證書時出現在問題。

 

再次用HttpAnalyzerStdV5查看是否已經加密。如下,已經加密了

 

image


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM