使用HttpWebRequest请求远端服务器时如何加载SSL证书
2012-06-29 16:02
776 查看
首先加上引用“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好了。
+++++++++++++++++++++++++++++++++
调用webservice时有的时候安全性要求比较高,wse提供了客户端证书来调用webservice,好我们就来看看怎么弄。
调用webservice当然有客户端和webservice端了,我们先来看看
客户端:
using Microsoft.Web.Services2.Security;
using Microsoft.Web.Services2.Security.Tokens;
using Microsoft.Web.Services2.Security.X509;
1。// 在个人证书存储区获取证书
X509CertificateStore store = X509CertificateStore.CurrentUserStore( X509CertificateStore.MyStore );
store.OpenRead()
//读取证书的keyid
X509CertificateCollection certs = store.FindCertificateByKeyIdentifier( Convert.FromBase64String( keyIdentifier ) );
X509SecurityToken token = null;
if (certs.Count > 0)
{
// 得到证书存储区的第一个个人证书
token = new X509SecurityToken( ((X509Certificate) certs[0]) );
}
2。//把token加入到soap中
ServiceWse serviceProxy = new ServiceWse(); //远程webservice代理
serviceProxy.RequestSoapContext.Security.Tokens.Add( token );
serviceProxy.RequestSoapContext.Security.Elements.Add( new MessageSignature( token ) );
3。调用webservice的方法:
。。。和普通调用webservice的方法一样,我这里就不说了:)
WebService端:
1。配置web.config
在configuration节点下加:<configSections>
<section name="microsoft.web.services2" type="Microsoft.Web.Services2.Configuration.WebServicesConfiguration, Microsoft.Web.Services2, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
</configSections>表示引用的是wse2.0
在<system.web>下加:<webServices>
<soapExtensionTypes>
<add type="Microsoft.Web.Services2.WebServicesExtension, Microsoft.Web.Services2, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" priority="1" group="0" />
</soapExtensionTypes>
</webServices>
在configuration节点下加:<microsoft.web.services2>
<security>
<x509 allowTestRoot="true" allowRevocationUrlRetrieval="false" verifyTrust="true" />
</security>
</microsoft.web.services2>
这个wse2.0中规定的xml节点。
2。验证客户端提交上来的证书
//获取客户端提交上来的证书
X509SecurityToken x509Token = GetSigningToken(RequestSoapContext.Current) as X509SecurityToken;
public SecurityToken GetSigningToken(SoapContext context)
{
foreach ( ISecurityElement element in context.Security.Elements )
{
if ( element is MessageSignature )
{
// The given context contains a Signature element.
MessageSignature sig = element as MessageSignature;
if (CheckSignature(context, sig))
{
// The SOAP Body is signed.
return sig.SigningToken;
}
}
}
return null;
}
//判断证书是否合法
//根据证书的keyid来判断
//这个就是证书的keyid,
x509Token.KeyIdentifier.Value
。。。
如果和你颁发的证书keyid不一致的话,你可以抛给他一个错误:
throw new SecurityFault(SecurityFault.FailedAuthenticationMessage, SecurityFault.FailedAuthenticationCode);
如果正确,执行webservice中的代码。
原帖:http://www.cnblogs.com/fooo/archive/2008/10/31/1323396.html
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好了。
+++++++++++++++++++++++++++++++++
调用webservice时有的时候安全性要求比较高,wse提供了客户端证书来调用webservice,好我们就来看看怎么弄。
调用webservice当然有客户端和webservice端了,我们先来看看
客户端:
using Microsoft.Web.Services2.Security;
using Microsoft.Web.Services2.Security.Tokens;
using Microsoft.Web.Services2.Security.X509;
1。// 在个人证书存储区获取证书
X509CertificateStore store = X509CertificateStore.CurrentUserStore( X509CertificateStore.MyStore );
store.OpenRead()
//读取证书的keyid
X509CertificateCollection certs = store.FindCertificateByKeyIdentifier( Convert.FromBase64String( keyIdentifier ) );
X509SecurityToken token = null;
if (certs.Count > 0)
{
// 得到证书存储区的第一个个人证书
token = new X509SecurityToken( ((X509Certificate) certs[0]) );
}
2。//把token加入到soap中
ServiceWse serviceProxy = new ServiceWse(); //远程webservice代理
serviceProxy.RequestSoapContext.Security.Tokens.Add( token );
serviceProxy.RequestSoapContext.Security.Elements.Add( new MessageSignature( token ) );
3。调用webservice的方法:
。。。和普通调用webservice的方法一样,我这里就不说了:)
WebService端:
1。配置web.config
在configuration节点下加:<configSections>
<section name="microsoft.web.services2" type="Microsoft.Web.Services2.Configuration.WebServicesConfiguration, Microsoft.Web.Services2, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
</configSections>表示引用的是wse2.0
在<system.web>下加:<webServices>
<soapExtensionTypes>
<add type="Microsoft.Web.Services2.WebServicesExtension, Microsoft.Web.Services2, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" priority="1" group="0" />
</soapExtensionTypes>
</webServices>
在configuration节点下加:<microsoft.web.services2>
<security>
<x509 allowTestRoot="true" allowRevocationUrlRetrieval="false" verifyTrust="true" />
</security>
</microsoft.web.services2>
这个wse2.0中规定的xml节点。
2。验证客户端提交上来的证书
//获取客户端提交上来的证书
X509SecurityToken x509Token = GetSigningToken(RequestSoapContext.Current) as X509SecurityToken;
public SecurityToken GetSigningToken(SoapContext context)
{
foreach ( ISecurityElement element in context.Security.Elements )
{
if ( element is MessageSignature )
{
// The given context contains a Signature element.
MessageSignature sig = element as MessageSignature;
if (CheckSignature(context, sig))
{
// The SOAP Body is signed.
return sig.SigningToken;
}
}
}
return null;
}
//判断证书是否合法
//根据证书的keyid来判断
//这个就是证书的keyid,
x509Token.KeyIdentifier.Value
。。。
如果和你颁发的证书keyid不一致的话,你可以抛给他一个错误:
throw new SecurityFault(SecurityFault.FailedAuthenticationMessage, SecurityFault.FailedAuthenticationCode);
如果正确,执行webservice中的代码。
原帖:http://www.cnblogs.com/fooo/archive/2008/10/31/1323396.html
相关文章推荐
- [C#]使用HttpWebRequest请求远端服务器时如何加载SSL证书
- [dotNET]使用HttpWebRequest请求远端服务器时如何加载SSL证书
- [dotNET]使用HttpWebRequest请求远端服务器时如何加载SSL证书
- [C#]使用HttpWebRequest请求远端服务器时如何加载SSL证书
- [C#]使用HttpWebRequest请求远端服务器时如何加载SSL证书
- [dotNET]使用HttpWebRequest请求远端服务器时如何加载SSL证书
- [dotNET]使用HttpWebRequest请求远端服务器时如何加载SSL证书
- HttpWebRequest加载证书请求远端https服务器时 : 基础连接已经关闭: 无法与远程服务器建立信任关系
- js_html_input中autocomplete="off"在chrom中失效的解决办法 使用JS模拟锚点跳转 js如何获取url参数 C#模拟httpwebrequest请求_向服务器模拟cookie发送 实习期学到的技术(一) LinqPad的变量比较功能 ASP.NET EF 使用LinqPad 快速学习Linq
- 【转】asp.net(c#)使用HttpWebRequest附加携带请求参数以post方式模拟上传大文件(以图片为例)到Web服务器端
- C#如何HttpWebRequest模拟登陆,获取服务端返回Cookie以便登录请求后使用
- asp.net(c#)使用HttpWebRequest附加携带请求参数以post方式模拟上传大文件(以图片为例)到Web服务器端
- 如何使用FtpWebRequest对ftp服务器上面的目录进行改名(注意是目录不是文件!)
- ASIHTTPRequest加载web请求
- 通过URL,使用HttpURLConnection进行请求/响应,访问web服务器图片等资源。异常报错
- 通过HttpWebRequest分别向服务器发送GET或POST请求
- 如何使用HttpWebRequest POST数据时设置Connection="Keep-Alive"
- 使用XMLHttpRequest对象向服务器发送简单的请求
- 如何使用ASIHTTPRequest从iOS端向服务器上传文件?