编写者:郑昀@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:这种现象如果换一台测试机器,是否依然出现呢?
新闻热点
疑难解答