[C#]使用HttpWebRequest請求遠端伺服器時如何載入SSL認證

來源:互聯網
上載者:User

編寫者:鄭昀@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

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.