編寫者:鄭昀@ultrapower
關鍵字:httpwebrequest,
ssl,x509certificate
dotnet framwork 1.1
編寫時間:2005-3-29
wse 2.0 sp3
目的:
對于用httpwebrequest加載證書請求遠端https服務器時,發生的
“基礎連接已經關閉: 無法與遠程服務器建立信任關系。”/
“the underlying connection was closed. could not establish a secure ssl/tls connection”錯誤,我們可以用如下方式解決。
重現:
使用以下代碼,你就可以得到這個錯誤“基礎連接已經關閉: 無法與遠程服務器建立信任關系”:
using system;
using system.text;
using system.net;
using system.io;
using system.security.cryptography.x509certificates;
using microsoft.web.services2.security;
using microsoft.web.services2.security.tokens;
using microsoft.web.services2.security.x509;
static void main(string[] args)
{
stringbuilder sb=new stringbuilder();
string _strtorequest = "send";
try
{
//post請求開始
byte[] bt=encoding.default.getbytes("send");
httpwebrequest req=(httpwebrequest)system.net.webrequest.create("https://202.108.ccc.xxx:port//");
req.keepalive=true;
//req.timeout=60000;
req.contenttype="text/xml";
req.contentlength=_strtorequest.length;
req.method="post";
x509certificatestore store = x509certificatestore.currentuserstore( x509certificatestore.mystore );
store.openread();
//讀取證書的keyid
microsoft.web.services2.security.x509.x509certificatecollection certs =
store.findcertificatebykeyidentifier( convert.frombase64string( "cxv+xz78zi3qwhgj6wh9bf6b23a=" ) );
x509securitytoken token = null;
if (certs.count > 0)
{
// 得到證書存儲區的第1個人證書
token = new x509securitytoken( ((microsoft.web.services2.security.x509.x509certificate) certs[0]) );
}
if(token != null)
req.clientcertificates.add(token.certificate);
req.keepalive=true;
stream reqstream=req.getrequeststream();
reqstream.write(bt,0,bt.length);
reqstream.close();
//得到響應
httpwebresponse res=(httpwebresponse)req.getresponse();
streamreader sr=new streamreader(res.getresponsestream(),encoding.default);
sb.append(sr.readtoend());
res.close();
sr.close();
}
catch(exception ex)
{
sb.remove(0,sb.length);
sb.append("<?xml version=/"1.0/" encoding=/"gb2312/"?>/n");
sb.append("<slia ver=/"1.0.0/">/n");
sb.append("<result resid=/"501/">"+ex.message+"</result>/n");
sb.append("</slia>/n");
}
console.writeline(sb.tostring());
console.read();
}
原因:
在“http://msdn.microsoft.com/library/chs/default.asp?url=/library/chs/cpguide/html/cpconhostingremoteobjectsininternetinformationservicesiis.asp”提到:
“
證書標識特定的計算機,該計算機的名稱位于證書的公共名稱中。但是,很容易就會更改計算機的名稱或使用客戶端配置文件中的“localhost”,這會在客戶端和服務器證書中的公共名稱之間造成不匹配的情況。在 .net framework 1.0 版中,這一不匹配的情況將被忽略,并且將在服務器上引發調用。
從 .net framework 1.1 版開始,這一不匹配的情況會引發以下異常:“system.net.webexception:基礎連接已經關閉:無法與遠程服務器建立信任關系”。如果您無法配置遠程處理客戶端以使用證書公共名稱,則可以使用客戶端應用程序配置文件中的以下設置重寫這一不匹配的情況。
<system.net>
<settings>
<servicepointmanager
checkcertificatename="true"
/>
</settings>
</system.net>
若要以編程方式使客戶端忽略證書名稱不匹配,客戶端必須創建一個特定類的實例,如果 certificateproblem 值為 0x800c010f,該類將實現 icertificatepolicy 接口并實現 checkvalidationresult 方法以返回 true。然后,您必須將該對象注冊到 system.net.servicepointmanager 對象,方法是將該對象傳遞到 servicepointmanager.certificatepolicy 屬性。”
解決之道:
但是用它列出的代碼還是不對,我們改為checkvalidationresult無條件返回true即可。如下所示聲明一個trustallcertificatepolicy類:
public class trustallcertificatepolicy : system.net.icertificatepolicy
{
public trustallcertificatepolicy()
{}
public bool checkvalidationresult(servicepoint sp,
system.security.cryptography.x509certificates.x509certificate cert,
webrequest req, int problem)
{
return true;
}
}
然后,在請求之前加上
system.net.servicepointmanager.certificatepolicy = new trustallcertificatepolicy();
即可。
這樣,代碼就可以順利和https服務器建立ssl通道了。
編寫者:鄭昀@ultrapower
feedback
# re: [c#]用httpwebrequest加載證書建立ssl通道時發生異常的解決辦法
2005-04-16 01:41 by lonelystranger
nice!
i have read your articles on csdn before.
# re: [c#]用httpwebrequest加載證書建立ssl通道時發生異常的解決辦法
2005-07-28 14:18 by caravarn
trustallcertificatepolicy 這個類具體怎么用啊,我還是
不明白我的 msn :[email protected]
# re: [c#]用httpwebrequest加載證書建立ssl通道時發生異常的解決辦法
2005-07-28 20:19 by bobomail
store.findcertificatebykeyidentifier( convert.frombase64string( "cxv+xz78zi3qwhgj6wh9bf6b23a=" ) );
這句話 里的cxv+xz78zi3qwhgj6wh9bf6b23a=" 就是證書號碼
這個在程序里怎么制定
很急啊 msn : [email protected]
# re: [c#]用httpwebrequest加載證書建立ssl通道時發生異常的解決辦法
2005-07-29 00:11 by 讓變化成為計劃的一部分
hi,
如果你鏈接的服務器并不要求你用證書,雖然你要走https,其實讀取證書的keyid 是不需要的。后來我們就沒有取,直接做請求,只要有
system.net.servicepointmanager.certificatepolicy = new trustallcertificatepolicy();
這句話,就可以了。
至于key id ,是通過wse .20中的x.509 證書工具查看的.
發件人: zhengyun
收件人: '[email protected]'
主題: 代碼如下所示
using microsoft.web.services2.security;
using microsoft.web.services2.security.tokens;
using microsoft.web.services2.security.x509;
//post請求開始
byte[] bt=encoding.default.getbytes(_strtorequest);
httpwebrequest req=(httpwebrequest)system.net.webrequest.create(_url);
req.keepalive=true;
req.contenttype="txt/xml";
req.method="post";
system.net.servicepointmanager.certificatepolicy = new trustallcertificatepolicy();
stream reqstream=req.getrequeststream();
.......................
internal class trustallcertificatepolicy:icertificatepolicy
{
public trustallcertificatepolicy()
{
//
// todo: 在此處添加構造函數邏輯
//
}
public bool checkvalidationresult(servicepoint sp,system.security.cryptography.x509certificates.x509certificate cert,system.net.webrequest req, int problem)
{
return true;// 這是最關鍵的,返回true,告訴客戶端忽略證書名稱不匹配!
}
}
# re: [c#]用httpwebrequest加載證書建立ssl通道時發生異常的解決辦法
2005-08-01 15:04 by caravarn
我請求的服務器是用java 開發 system.net.servicepointmanager.certificatepolicy = new trustallcertificatepolicy(); 調試中也 return true
但還是提示連接失敗?
這是原因呢?
[email protected]
# re: [c#]用httpwebrequest加載證書建立ssl通道時發生異常的解決辦法
2005-08-17 11:14 by caravarn
返回的錯誤
problem code 0x800b0109
problem code 0x800b0101
problem code 0x800b010f
problem code 0x00000000
對應的錯誤
public enum certificateproblem : long
{
certexpired = 0x800b0101,
certvalidityperiodnesting = 0x800b0102,
certrole = 0x800b0103,
certpathlenconst = 0x800b0104,
certcritical = 0x800b0105,
certpurpose = 0x800b0106,
certissuerchaining = 0x800b0107,
certmalformed = 0x800b0108,
certuntrustedroot = 0x800b0109,
certchaining = 0x800b010a,
certrevoked = 0x800b010c,
certuntrustedtestroot = 0x800b010d,
certrevocation_failure = 0x800b010e,
certcn_no_match = 0x800b010f,
certwrong_usage = 0x800b0110,
certuntrustedca = 0x800b0112
}
這個不知道怎么解決阿
# re: [c#]用httpwebrequest加載證書建立ssl通道時發生異常的解決辦法
2005-08-18 09:45 by 讓變化成為計劃的一部分
錯誤號:0x800b0109的意思是:
a certificate chain processed, but terminated in a root certificate which is
not trusted by the trust provider
也就是說,你的ca cert并沒有被對方所信任。你不覺得很奇怪嗎?你裝對方站點提供的證書了嗎?
至于0x800b0101的意思是 :a required certificate is not within its validity period when
verifying against the current system clock or the timestamp in the signed
file.
那么請你確定幾點:
1:詢問對方是否需要證書?
2:你的時鐘和對方的時間是否差距在5分鐘之內?
3:這種現象如果換一臺測試機器,是否依然出現呢?
新聞熱點
疑難解答