編寫者:鄭昀@UltraPower
首先加上引用“System.Security.DLL”, 其次在工程中
using System.Security.Cryptography.X509Certificates;
這樣就可以使用“
X509Certificate Class
”了,它的定義參見http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemsecuritycryptographyx509certificatesx509certificateclasstopic.asp。
之後我們就可以
/// 構建請求的HttpWebRequest對象
HttpWebRequest hwrRequest = (HttpWebRequest)WebRequest.Create(
strValidatePageURL);
/// 從本地檔案中載入認證
hwrRequest.ClientCertificates.Add(X509Certificate.CreateFromCertFile("c:\\motor.https.pem.cer"));
這是一個較簡單的辦法。
如果你遇到了“The underlying connection was closed. Could not establish a secure SSL/TLS connection"”的異常,那麼請設定
hwrRequest.KeepAlive = true;
如果您使用的是CreateFromSignedFile來建立認證,那麼請您務必注意,即使CreateFromSignedFile沒有能夠從檔案中建立認證,甚至即使沒有找到該檔案,他也不會拋出異常,也不返回null,只是他的各個欄位為null。
所以,。。。,還是請使用CreateFromCertFile好了。
至於如何“在個人憑證存放區區擷取認證”,參看下面的blog:
參看: WSE2.0中X509安全性權杖的使用
和 調用web service如何載入認證 .
編寫者:鄭昀@UltraPower
20050328