- 浏览: 4193423 次
最新评论
[dotNET]使用HttpWebRequest请求远端服务器时如何加载SSL证书
使用HttpWebRequest请求远端服务器时如何加载SSL证书
编写者:郑昀@UltraPower
首先加上引用“System.Security.DLL”,
其次在工程中
using System.Security.Cryptography.X509Certificates;
这样就可以使用“
X509Certificate Class
之后我们就可以
/// 构建请求的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发布了,大家都来一起研究吧,我先来抛砖引玉:
调用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><br><section name="microsoft.web.services2" type="Microsoft.Web.Services2.Configuration.WebServicesConfiguration, Microsoft.Web.Services2, Version=<chsdate w:st=" on isrocdate="False" islunardate="False" day="30" month="12" year="1899">2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" /><br></section></configsections>表示引用的是wse2.0
在<system.web></system.web>下加:<webservices><br><soapextensiontypes><br><add type="Microsoft.Web.Services2.WebServicesExtension, Microsoft.Web.Services2, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" priority="1" group="0"></add><br></soapextensiontypes><br></webservices>
在configuration节点下加:<microsoft.web.services2><br><security><br><x509 allowtestroot="true" allowrevocationurlretrieval="false" verifytrust="true"></x509><br></security><br></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中的代码。
在调用web service时,如果web service需要客户端证书,也就是需要走ssl协议,那么在调用的时候就需要加载上一个客户端证书,这个客户端证书是一个.cer文件,可以从浏览器的证书中导出,在导出的时候不用导出私钥,这样导出的证书是不包含私钥的,也即这个证书文件拷贝到其它机器是无效的。
在调用的时候比较简单。如下:
// The path to the certificate.
string Certificate = "Certificate.cer";
// Load the certificate into an X509Certificate object.
X509Certificate cert = X509Certificate.CreateFromCertFile(Certificate);
LoginService srv = new LoginService();
srv.ClientCertificates.Add(cert);
如果不加载这个证书就会返回403禁止访问错误。
另外为了避免每次new的时候都采用代码加载证书,可以直接修改ws的代理类,比如:
public LoginService() {
string urlSetting = System.Configuration.ConfigurationSettings.AppSettings["LoginPlugin.localhost.LoginService"];
if ((urlSetting != null)) {
this.Url = string.Concat(urlSetting, "");
}
else {
this.Url = "http://localhost/Jiancha2/Services/LoginService.asmx";
}
if (System.Configuration.ConfigurationSettings.AppSettings["ssl"] == "true" && Ocean.Plugins.CertInfo.Cert != null)
{
this.ClientCertificates.Add(Ocean.Plugins.CertInfo.Cert);
}
}
至于证书服务器和web服务器如何支持ssl,这个在dev-club的电子杂志上有一期有专门的讲解,我就不多说了。
编写者:郑昀@UltraPower 20050328
编写者:郑昀@UltraPower
20050328
使用HttpWebRequest请求远端服务器时如何加载SSL证书
编写者:郑昀@UltraPower
首先加上引用“System.Security.DLL”,
其次在工程中
using System.Security.Cryptography.X509Certificates;
这样就可以使用“
X509Certificate Class
之后我们就可以
/// 构建请求的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发布了,大家都来一起研究吧,我先来抛砖引玉:
调用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><br><section name="microsoft.web.services2" type="Microsoft.Web.Services2.Configuration.WebServicesConfiguration, Microsoft.Web.Services2, Version=<chsdate w:st=" on isrocdate="False" islunardate="False" day="30" month="12" year="1899">2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" /><br></section></configsections>表示引用的是wse2.0
在<system.web></system.web>下加:<webservices><br><soapextensiontypes><br><add type="Microsoft.Web.Services2.WebServicesExtension, Microsoft.Web.Services2, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" priority="1" group="0"></add><br></soapextensiontypes><br></webservices>
在configuration节点下加:<microsoft.web.services2><br><security><br><x509 allowtestroot="true" allowrevocationurlretrieval="false" verifytrust="true"></x509><br></security><br></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中的代码。
在调用web service时,如果web service需要客户端证书,也就是需要走ssl协议,那么在调用的时候就需要加载上一个客户端证书,这个客户端证书是一个.cer文件,可以从浏览器的证书中导出,在导出的时候不用导出私钥,这样导出的证书是不包含私钥的,也即这个证书文件拷贝到其它机器是无效的。
在调用的时候比较简单。如下:
// The path to the certificate.
string Certificate = "Certificate.cer";
// Load the certificate into an X509Certificate object.
X509Certificate cert = X509Certificate.CreateFromCertFile(Certificate);
LoginService srv = new LoginService();
srv.ClientCertificates.Add(cert);
如果不加载这个证书就会返回403禁止访问错误。
另外为了避免每次new的时候都采用代码加载证书,可以直接修改ws的代理类,比如:
public LoginService() {
string urlSetting = System.Configuration.ConfigurationSettings.AppSettings["LoginPlugin.localhost.LoginService"];
if ((urlSetting != null)) {
this.Url = string.Concat(urlSetting, "");
}
else {
this.Url = "http://localhost/Jiancha2/Services/LoginService.asmx";
}
if (System.Configuration.ConfigurationSettings.AppSettings["ssl"] == "true" && Ocean.Plugins.CertInfo.Cert != null)
{
this.ClientCertificates.Add(Ocean.Plugins.CertInfo.Cert);
}
}
至于证书服务器和web服务器如何支持ssl,这个在dev-club的电子杂志上有一期有专门的讲解,我就不多说了。
编写者:郑昀@UltraPower 20050328
编写者:郑昀@UltraPower
20050328
使用HttpWebRequest请求远端服务器时如何加载SSL证书
编写者:郑昀@UltraPower
首先加上引用“System.Security.DLL”,
其次在工程中
using System.Security.Cryptography.X509Certificates;
这样就可以使用“
X509Certificate Class
之后我们就可以
/// 构建请求的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发布了,大家都来一起研究吧,我先来抛砖引玉:
调用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><br><section name="microsoft.web.services2" type="Microsoft.Web.Services2.Configuration.WebServicesConfiguration, Microsoft.Web.Services2, Version=<chsdate w:st=" on isrocdate="False" islunardate="False" day="30" month="12" year="1899">2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" /><br></section></configsections>表示引用的是wse2.0
在<system.web></system.web>下加:<webservices><br><soapextensiontypes><br><add type="Microsoft.Web.Services2.WebServicesExtension, Microsoft.Web.Services2, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" priority="1" group="0"></add><br></soapextensiontypes><br></webservices>
在configuration节点下加:<microsoft.web.services2><br><security><br><x509 allowtestroot="true" allowrevocationurlretrieval="false" verifytrust="true"></x509><br></security><br></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中的代码。
在调用web service时,如果web service需要客户端证书,也就是需要走ssl协议,那么在调用的时候就需要加载上一个客户端证书,这个客户端证书是一个.cer文件,可以从浏览器的证书中导出,在导出的时候不用导出私钥,这样导出的证书是不包含私钥的,也即这个证书文件拷贝到其它机器是无效的。
在调用的时候比较简单。如下:
// The path to the certificate.
string Certificate = "Certificate.cer";
// Load the certificate into an X509Certificate object.
X509Certificate cert = X509Certificate.CreateFromCertFile(Certificate);
LoginService srv = new LoginService();
srv.ClientCertificates.Add(cert);
如果不加载这个证书就会返回403禁止访问错误。
另外为了避免每次new的时候都采用代码加载证书,可以直接修改ws的代理类,比如:
public LoginService() {
string urlSetting = System.Configuration.ConfigurationSettings.AppSettings["LoginPlugin.localhost.LoginService"];
if ((urlSetting != null)) {
this.Url = string.Concat(urlSetting, "");
}
else {
this.Url = "http://localhost/Jiancha2/Services/LoginService.asmx";
}
if (System.Configuration.ConfigurationSettings.AppSettings["ssl"] == "true" && Ocean.Plugins.CertInfo.Cert != null)
{
this.ClientCertificates.Add(Ocean.Plugins.CertInfo.Cert);
}
}
至于证书服务器和web服务器如何支持ssl,这个在dev-club的电子杂志上有一期有专门的讲解,我就不多说了。
编写者:郑昀@UltraPower 20050328
编写者:郑昀@UltraPower
20050328
使用HttpWebRequest请求远端服务器时如何加载SSL证书
编写者:郑昀@UltraPower
首先加上引用“System.Security.DLL”,
其次在工程中
using System.Security.Cryptography.X509Certificates;
这样就可以使用“
X509Certificate Class
之后我们就可以
/// 构建请求的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发布了,大家都来一起研究吧,我先来抛砖引玉:
调用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><br><section name="microsoft.web.services2" type="Microsoft.Web.Services2.Configuration.WebServicesConfiguration, Microsoft.Web.Services2, Version=<chsdate w:st=" on isrocdate="False" islunardate="False" day="30" month="12" year="1899">2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" /><br></section></configsections>表示引用的是wse2.0
在<system.web></system.web>下加:<webservices><br><soapextensiontypes><br><add type="Microsoft.Web.Services2.WebServicesExtension, Microsoft.Web.Services2, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" priority="1" group="0"></add><br></soapextensiontypes><br></webservices>
在configuration节点下加:<microsoft.web.services2><br><security><br><x509 allowtestroot="true" allowrevocationurlretrieval="false" verifytrust="true"></x509><br></security><br></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中的代码。
在调用web service时,如果web service需要客户端证书,也就是需要走ssl协议,那么在调用的时候就需要加载上一个客户端证书,这个客户端证书是一个.cer文件,可以从浏览器的证书中导出,在导出的时候不用导出私钥,这样导出的证书是不包含私钥的,也即这个证书文件拷贝到其它机器是无效的。
在调用的时候比较简单。如下:
// The path to the certificate.
string Certificate = "Certificate.cer";
// Load the certificate into an X509Certificate object.
X509Certificate cert = X509Certificate.CreateFromCertFile(Certificate);
LoginService srv = new LoginService();
srv.ClientCertificates.Add(cert);
如果不加载这个证书就会返回403禁止访问错误。
另外为了避免每次new的时候都采用代码加载证书,可以直接修改ws的代理类,比如:
public LoginService() {
string urlSetting = System.Configuration.ConfigurationSettings.AppSettings["LoginPlugin.localhost.LoginService"];
if ((urlSetting != null)) {
this.Url = string.Concat(urlSetting, "");
}
else {
this.Url = "http://localhost/Jiancha2/Services/LoginService.asmx";
}
if (System.Configuration.ConfigurationSettings.AppSettings["ssl"] == "true" && Ocean.Plugins.CertInfo.Cert != null)
{
this.ClientCertificates.Add(Ocean.Plugins.CertInfo.Cert);
}
}
至于证书服务器和web服务器如何支持ssl,这个在dev-club的电子杂志上有一期有专门的讲解,我就不多说了。
编写者:郑昀@UltraPower 20050328
编写者:郑昀@UltraPower
20050328
使用HttpWebRequest请求远端服务器时如何加载SSL证书
编写者:郑昀@UltraPower
首先加上引用“System.Security.DLL”,
其次在工程中
using System.Security.Cryptography.X509Certificates;
这样就可以使用“
X509Certificate Class
之后我们就可以
/// 构建请求的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发布了,大家都来一起研究吧,我先来抛砖引玉:
调用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><br><section name="microsoft.web.services2" type="Microsoft.Web.Services2.Configuration.WebServicesConfiguration, Microsoft.Web.Services2, Version=<chsdate w:st=" on isrocdate="False" islunardate="False" day="30" month="12" year="1899">2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" /><br></section></configsections>表示引用的是wse2.0
在<system.web></system.web>下加:<webservices><br><soapextensiontypes><br><add type="Microsoft.Web.Services2.WebServicesExtension, Microsoft.Web.Services2, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" priority="1" group="0"></add><br></soapextensiontypes><br></webservices>
在configuration节点下加:<microsoft.web.services2><br><security><br><x509 allowtestroot="true" allowrevocationurlretrieval="false" verifytrust="true"></x509><br></security><br></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中的代码。
在调用web service时,如果web service需要客户端证书,也就是需要走ssl协议,那么在调用的时候就需要加载上一个客户端证书,这个客户端证书是一个.cer文件,可以从浏览器的证书中导出,在导出的时候不用导出私钥,这样导出的证书是不包含私钥的,也即这个证书文件拷贝到其它机器是无效的。
在调用的时候比较简单。如下:
// The path to the certificate.
string Certificate = "Certificate.cer";
// Load the certificate into an X509Certificate object.
X509Certificate cert = X509Certificate.CreateFromCertFile(Certificate);
LoginService srv = new LoginService();
srv.ClientCertificates.Add(cert);
如果不加载这个证书就会返回403禁止访问错误。
另外为了避免每次new的时候都采用代码加载证书,可以直接修改ws的代理类,比如:
public LoginService() {
string urlSetting = System.Configuration.ConfigurationSettings.AppSettings["LoginPlugin.localhost.LoginService"];
if ((urlSetting != null)) {
this.Url = string.Concat(urlSetting, "");
}
else {
this.Url = "http://localhost/Jiancha2/Services/LoginService.asmx";
}
if (System.Configuration.ConfigurationSettings.AppSettings["ssl"] == "true" && Ocean.Plugins.CertInfo.Cert != null)
{
this.ClientCertificates.Add(Ocean.Plugins.CertInfo.Cert);
}
}
至于证书服务器和web服务器如何支持ssl,这个在dev-club的电子杂志上有一期有专门的讲解,我就不多说了。
编写者:郑昀@UltraPower 20050328
编写者:郑昀@UltraPower
20050328
使用HttpWebRequest请求远端服务器时如何加载SSL证书
编写者:郑昀@UltraPower
首先加上引用“System.Security.DLL”,
其次在工程中
using System.Security.Cryptography.X509Certificates;
这样就可以使用“
X509Certificate Class
之后我们就可以
/// 构建请求的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发布了,大家都来一起研究吧,我先来抛砖引玉:
调用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><br><section name="microsoft.web.services2" type="Microsoft.Web.Services2.Configuration.WebServicesConfiguration, Microsoft.Web.Services2, Version=<chsdate w:st=" on isrocdate="False" islunardate="False" day="30" month="12" year="1899">2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" /><br></section></configsections>表示引用的是wse2.0
在<system.web></system.web>下加:<webservices><br><soapextensiontypes><br><add type="Microsoft.Web.Services2.WebServicesExtension, Microsoft.Web.Services2, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" priority="1" group="0"></add><br></soapextensiontypes><br></webservices>
在configuration节点下加:<microsoft.web.services2><br><security><br><x509 allowtestroot="true" allowrevocationurlretrieval="false" verifytrust="true"></x509><br></security><br></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中的代码。
在调用web service时,如果web service需要客户端证书,也就是需要走ssl协议,那么在调用的时候就需要加载上一个客户端证书,这个客户端证书是一个.cer文件,可以从浏览器的证书中导出,在导出的时候不用导出私钥,这样导出的证书是不包含私钥的,也即这个证书文件拷贝到其它机器是无效的。
在调用的时候比较简单。如下:
// The path to the certificate.
string Certificate = "Certificate.cer";
// Load the certificate into an X509Certificate object.
X509Certificate cert = X509Certificate.CreateFromCertFile(Certificate);
LoginService srv = new LoginService();
srv.ClientCertificates.Add(cert);
如果不加载这个证书就会返回403禁止访问错误。
另外为了避免每次new的时候都采用代码加载证书,可以直接修改ws的代理类,比如:
public LoginService() {
string urlSetting = System.Configuration.ConfigurationSettings.AppSettings["LoginPlugin.localhost.LoginService"];
if ((urlSetting != null)) {
this.Url = string.Concat(urlSetting, "");
}
else {
this.Url = "http://localhost/Jiancha2/Services/LoginService.asmx";
}
if (System.Configuration.ConfigurationSettings.AppSettings["ssl"] == "true" && Ocean.Plugins.CertInfo.Cert != null)
{
this.ClientCertificates.Add(Ocean.Plugins.CertInfo.Cert);
}
}
至于证书服务器和web服务器如何支持ssl,这个在dev-club的电子杂志上有一期有专门的讲解,我就不多说了。
编写者:郑昀@UltraPower 20050328
编写者:郑昀@UltraPower
20050328
使用HttpWebRequest请求远端服务器时如何加载SSL证书
编写者:郑昀@UltraPower
首先加上引用“System.Security.DLL”,
其次在工程中
using System.Security.Cryptography.X509Certificates;
这样就可以使用“
X509Certificate Class
之后我们就可以
/// 构建请求的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发布了,大家都来一起研究吧,我先来抛砖引玉:
调用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><br><section name="microsoft.web.services2" type="Microsoft.Web.Services2.Configuration.WebServicesConfiguration, Microsoft.Web.Services2, Version=<chsdate w:st=" on isrocdate="False" islunardate="False" day="30" month="12" year="1899">2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" /><br></section></configsections>表示引用的是wse2.0
在<system.web></system.web>下加:<webservices><br><soapextensiontypes><br><add type="Microsoft.Web.Services2.WebServicesExtension, Microsoft.Web.Services2, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" priority="1" group="0"></add><br></soapextensiontypes><br></webservices>
在configuration节点下加:<microsoft.web.services2><br><security><br><x509 allowtestroot="true" allowrevocationurlretrieval="false" verifytrust="true"></x509><br></security><br></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中的代码。
在调用web service时,如果web service需要客户端证书,也就是需要走ssl协议,那么在调用的时候就需要加载上一个客户端证书,这个客户端证书是一个.cer文件,可以从浏览器的证书中导出,在导出的时候不用导出私钥,这样导出的证书是不包含私钥的,也即这个证书文件拷贝到其它机器是无效的。
在调用的时候比较简单。如下:
// The path to the certificate.
string Certificate = "Certificate.cer";
// Load the certificate into an X509Certificate object.
X509Certificate cert = X509Certificate.CreateFromCertFile(Certificate);
LoginService srv = new LoginService();
srv.ClientCertificates.Add(cert);
如果不加载这个证书就会返回403禁止访问错误。
另外为了避免每次new的时候都采用代码加载证书,可以直接修改ws的代理类,比如:
public LoginService() {
string urlSetting = System.Configuration.ConfigurationSettings.AppSettings["LoginPlugin.localhost.LoginService"];
if ((urlSetting != null)) {
this.Url = string.Concat(urlSetting, "");
}
else {
this.Url = "http://localhost/Jiancha2/Services/LoginService.asmx";
}
if (System.Configuration.ConfigurationSettings.AppSettings["ssl"] == "true" && Ocean.Plugins.CertInfo.Cert != null)
{
this.ClientCertificates.Add(Ocean.Plugins.CertInfo.Cert);
}
}
至于证书服务器和web服务器如何支持ssl,这个在dev-club的电子杂志上有一期有专门的讲解,我就不多说了。
编写者:郑昀@UltraPower 20050328
编写者:郑昀@UltraPower
20050328
使用HttpWebRequest请求远端服务器时如何加载SSL证书
编写者:郑昀@UltraPower
首先加上引用“System.Security.DLL”,
其次在工程中
using System.Security.Cryptography.X509Certificates;
这样就可以使用“
X509Certificate Class
之后我们就可以
/// 构建请求的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发布了,大家都来一起研究吧,我先来抛砖引玉:
调用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><br><section name="microsoft.web.services2" type="Microsoft.Web.Services2.Configuration.WebServicesConfiguration, Microsoft.Web.Services2, Version=<chsdate w:st=" on isrocdate="False" islunardate="False" day="30" month="12" year="1899">2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" /><br></section></configsections>表示引用的是wse2.0
在<system.web></system.web>下加:<webservices><br><soapextensiontypes><br><add type="Microsoft.Web.Services2.WebServicesExtension, Microsoft.Web.Services2, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" priority="1" group="0"></add><br></soapextensiontypes><br></webservices>
在configuration节点下加:<microsoft.web.services2><br><security><br><x509 allowtestroot="true" allowrevocationurlretrieval="false" verifytrust="true"></x509><br></security><br></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中的代码。
在调用web service时,如果web service需要客户端证书,也就是需要走ssl协议,那么在调用的时候就需要加载上一个客户端证书,这个客户端证书是一个.cer文件,可以从浏览器的证书中导出,在导出的时候不用导出私钥,这样导出的证书是不包含私钥的,也即这个证书文件拷贝到其它机器是无效的。
在调用的时候比较简单。如下:
// The path to the certificate.
string Certificate = "Certificate.cer";
// Load the certificate into an X509Certificate object.
X509Certificate cert = X509Certificate.CreateFromCertFile(Certificate);
LoginService srv = new LoginService();
srv.ClientCertificates.Add(cert);
如果不加载这个证书就会返回403禁止访问错误。
另外为了避免每次new的时候都采用代码加载证书,可以直接修改ws的代理类,比如:
public LoginService() {
string urlSetting = System.Configuration.ConfigurationSettings.AppSettings["LoginPlugin.localhost.LoginService"];
if ((urlSetting != null)) {
this.Url = string.Concat(urlSetting, "");
}
else {
this.Url = "http://localhost/Jiancha2/Services/LoginService.asmx";
}
if (System.Configuration.ConfigurationSettings.AppSettings["ssl"] == "true" && Ocean.Plugins.CertInfo.Cert != null)
{
this.ClientCertificates.Add(Ocean.Plugins.CertInfo.Cert);
}
}
至于证书服务器和web服务器如何支持ssl,这个在dev-club的电子杂志上有一期有专门的讲解,我就不多说了。
编写者:郑昀@UltraPower 20050328
编写者:郑昀@UltraPower
20050328
使用HttpWebRequest请求远端服务器时如何加载SSL证书
编写者:郑昀@UltraPower
首先加上引用“System.Security.DLL”,
其次在工程中
using System.Security.Cryptography.X509Certificates;
这样就可以使用“
X509Certificate Class
之后我们就可以
/// 构建请求的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发布了,大家都来一起研究吧,我先来抛砖引玉:
调用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><br><section name="microsoft.web.services2" type="Microsoft.Web.Services2.Configuration.WebServicesConfiguration, Microsoft.Web.Services2, Version=<chsdate w:st=" on isrocdate="False" islunardate="False" day="30" month="12" year="1899">2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" /><br></section></configsections>表示引用的是wse2.0
在<system.web></system.web>下加:<webservices><br><soapextensiontypes><br><add type="Microsoft.Web.Services2.WebServicesExtension, Microsoft.Web.Services2, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" priority="1" group="0"></add><br></soapextensiontypes><br></webservices>
在configuration节点下加:<microsoft.web.services2><br><security><br><x509 allowtestroot="true" allowrevocationurlretrieval="false" verifytrust="true"></x509><br></security><br></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中的代码。
在调用web service时,如果web service需要客户端证书,也就是需要走ssl协议,那么在调用的时候就需要加载上一个客户端证书,这个客户端证书是一个.cer文件,可以从浏览器的证书中导出,在导出的时候不用导出私钥,这样导出的证书是不包含私钥的,也即这个证书文件拷贝到其它机器是无效的。
在调用的时候比较简单。如下:
// The path to the certificate.
string Certificate = "Certificate.cer";
// Load the certificate into an X509Certificate object.
X509Certificate cert = X509Certificate.CreateFromCertFile(Certificate);
LoginService srv = new LoginService();
srv.ClientCertificates.Add(cert);
如果不加载这个证书就会返回403禁止访问错误。
另外为了避免每次new的时候都采用代码加载证书,可以直接修改ws的代理类,比如:
public LoginService() {
string urlSetting = System.Configuration.ConfigurationSettings.AppSettings["LoginPlugin.localhost.LoginService"];
if ((urlSetting != null)) {
this.Url = string.Concat(urlSetting, "");
}
else {
this.Url = "http://localhost/Jiancha2/Services/LoginService.asmx";
}
if (System.Configuration.ConfigurationSettings.AppSettings["ssl"] == "true" && Ocean.Plugins.CertInfo.Cert != null)
{
this.ClientCertificates.Add(Ocean.Plugins.CertInfo.Cert);
}
}
至于证书服务器和web服务器如何支持ssl,这个在dev-club的电子杂志上有一期有专门的讲解,我就不多说了。
编写者:郑昀@UltraPower 20050328
编写者:郑昀@UltraPower
20050328
使用HttpWebRequest请求远端服务器时如何加载SSL证书
编写者:郑昀@UltraPower
首先加上引用“System.Security.DLL”,
其次在工程中
using System.Security.Cryptography.X509Certificates;
这样就可以使用“
X509Certificate Class
之后我们就可以
/// 构建请求的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发布了,大家都来一起研究吧,我先来抛砖引玉:
调用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><br><section name="microsoft.web.services2" type="Microsoft.Web.Services2.Configuration.WebServicesConfiguration, Microsoft.Web.Services2, Version=<chsdate w:st=" on isrocdate="False" islunardate="False" day="30" month="12" year="1899">2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" /><br></section></configsections>表示引用的是wse2.0
在<system.web></system.web>下加:<webservices><br><soapextensiontypes><br><add type="Microsoft.Web.Services2.WebServicesExtension, Microsoft.Web.Services2, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" priority="1" group="0"></add><br></soapextensiontypes><br></webservices>
在configuration节点下加:<microsoft.web.services2><br><security><br><x509 allowtestroot="true" allowrevocationurlretrieval="false" verifytrust="true"></x509><br></security><br></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中的代码。
在调用web service时,如果web service需要客户端证书,也就是需要走ssl协议,那么在调用的时候就需要加载上一个客户端证书,这个客户端证书是一个.cer文件,可以从浏览器的证书中导出,在导出的时候不用导出私钥,这样导出的证书是不包含私钥的,也即这个证书文件拷贝到其它机器是无效的。
在调用的时候比较简单。如下:
// The path to the certificate.
string Certificate = "Certificate.cer";
// Load the certificate into an X509Certificate object.
X509Certificate cert = X509Certificate.CreateFromCertFile(Certificate);
LoginService srv = new LoginService();
srv.ClientCertificates.Add(cert);
如果不加载这个证书就会返回403禁止访问错误。
另外为了避免每次new的时候都采用代码加载证书,可以直接修改ws的代理类,比如:
public LoginService() {
string urlSetting = System.Configuration.ConfigurationSettings.AppSettings["LoginPlugin.localhost.LoginService"];
if ((urlSetting != null)) {
this.Url = string.Concat(urlSetting, "");
}
else {
this.Url = "http://localhost/Jiancha2/Services/LoginService.asmx";
}
if (System.Configuration.ConfigurationSettings.AppSettings["ssl"] == "true" && Ocean.Plugins.CertInfo.Cert != null)
{
this.ClientCertificates.Add(Ocean.Plugins.CertInfo.Cert);
}
}
至于证书服务器和web服务器如何支持ssl,这个在dev-club的电子杂志上有一期有专门的讲解,我就不多说了。
编写者:郑昀@UltraPower 20050328
编写者:郑昀@UltraPower
20050328
使用HttpWebRequest请求远端服务器时如何加载SSL证书
编写者:郑昀@UltraPower
首先加上引用“System.Security.DLL”,
其次在工程中
using System.Security.Cryptography.X509Certificates;
这样就可以使用“
X509Certificate Class
之后我们就可以
/// 构建请求的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发布了,大家都来一起研究吧,我先来抛砖引玉:
调用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><br><section name="microsoft.web.services2" type="Microsoft.Web.Services2.Configuration.WebServicesConfiguration, Microsoft.Web.Services2, Version=<chsdate w:st=" on isrocdate="False" islunardate="False" day="30" month="12" year="1899">2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" /><br></section></configsections>表示引用的是wse2.0
在<system.web></system.web>下加:<webservices><br><soapextensiontypes><br><add type="Microsoft.Web.Services2.WebServicesExtension, Microsoft.Web.Services2, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" priority="1" group="0"></add><br></soapextensiontypes><br></webservices>
在configuration节点下加:<microsoft.web.services2><br><security><br><x509 allowtestroot="true" allowrevocationurlretrieval="false" verifytrust="true"></x509><br></security><br></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中的代码。
在调用web service时,如果web service需要客户端证书,也就是需要走ssl协议,那么在调用的时候就需要加载上一个客户端证书,这个客户端证书是一个.cer文件,可以从浏览器的证书中导出,在导出的时候不用导出私钥,这样导出的证书是不包含私钥的,也即这个证书文件拷贝到其它机器是无效的。
在调用的时候比较简单。如下:
// The path to the certificate.
string Certificate = "Certificate.cer";
// Load the certificate into an X509Certificate object.
X509Certificate cert = X509Certificate.CreateFromCertFile(Certificate);
LoginService srv = new LoginService();
srv.ClientCertificates.Add(cert);
如果不加载这个证书就会返回403禁止访问错误。
另外为了避免每次new的时候都采用代码加载证书,可以直接修改ws的代理类,比如:
public LoginService() {
string urlSetting = System.Configuration.ConfigurationSettings.AppSettings["LoginPlugin.localhost.LoginService"];
if ((urlSetting != null)) {
this.Url = string.Concat(urlSetting, "");
}
else {
this.Url = "http://localhost/Jiancha2/Services/LoginService.asmx";
}
if (System.Configuration.ConfigurationSettings.AppSettings["ssl"] == "true" && Ocean.Plugins.CertInfo.Cert != null)
{
this.ClientCertificates.Add(Ocean.Plugins.CertInfo.Cert);
}
}
至于证书服务器和web服务器如何支持ssl,这个在dev-club的电子杂志上有一期有专门的讲解,我就不多说了。
编写者:郑昀@UltraPower 20050328
编写者:郑昀@UltraPower
20050328
使用HttpWebRequest请求远端服务器时如何加载SSL证书
编写者:郑昀@UltraPower
首先加上引用“System.Security.DLL”,
其次在工程中
using System.Security.Cryptography.X509Certificates;
这样就可以使用“
X509Certificate Class
之后我们就可以
/// 构建请求的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发布了,大家都来一起研究吧,我先来抛砖引玉:
调用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><br><section name="microsoft.web.services2" type="Microsoft.Web.Services2.Configuration.WebServicesConfiguration, Microsoft.Web.Services2, Version=<chsdate w:st=" on isrocdate="False" islunardate="False" day="30" month="12" year="1899">2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" /><br></section></configsections>表示引用的是wse2.0
在<system.web></system.web>下加:<webservices><br><soapextensiontypes><br><add type="Microsoft.Web.Services2.WebServicesExtension, Microsoft.Web.Services2, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" priority="1" group="0"></add><br></soapextensiontypes><br></webservices>
在configuration节点下加:<microsoft.web.services2><br><security><br><x509 allowtestroot="true" allowrevocationurlretrieval="false" verifytrust="true"></x509><br></security><br></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中的代码。
在调用web service时,如果web service需要客户端证书,也就是需要走ssl协议,那么在调用的时候就需要加载上一个客户端证书,这个客户端证书是一个.cer文件,可以从浏览器的证书中导出,在导出的时候不用导出私钥,这样导出的证书是不包含私钥的,也即这个证书文件拷贝到其它机器是无效的。
在调用的时候比较简单。如下:
// The path to the certificate.
string Certificate = "Certificate.cer";
// Load the certificate into an X509Certificate object.
X509Certificate cert = X509Certificate.CreateFromCertFile(Certificate);
LoginService srv = new LoginService();
srv.ClientCertificates.Add(cert);
如果不加载这个证书就会返回403禁止访问错误。
另外为了避免每次new的时候都采用代码加载证书,可以直接修改ws的代理类,比如:
public LoginService() {
string urlSetting = System.Configuration.ConfigurationSettings.AppSettings["LoginPlugin.localhost.LoginService"];
if ((urlSetting != null)) {
this.Url = string.Concat(urlSetting, "");
}
else {
this.Url = "http://localhost/Jiancha2/Services/LoginService.asmx";
}
if (System.Configuration.ConfigurationSettings.AppSettings["ssl"] == "true" && Ocean.Plugins.CertInfo.Cert != null)
{
this.ClientCertificates.Add(Ocean.Plugins.CertInfo.Cert);
}
}
至于证书服务器和web服务器如何支持ssl,这个在dev-club的电子杂志上有一期有专门的讲解,我就不多说了。
编写者:郑昀@UltraPower 20050328
编写者:郑昀@UltraPower
20050328
使用HttpWebRequest请求远端服务器时如何加载SSL证书
编写者:郑昀@UltraPower
首先加上引用“System.Security.DLL”,
其次在工程中
using System.Security.Cryptography.X509Certificates;
这样就可以使用“
X509Certificate Class
之后我们就可以
/// 构建请求的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发布了,大家都来一起研究吧,我先来抛砖引玉:
调用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><br><section name="microsoft.web.services2" type="Microsoft.Web.Services2.Configuration.WebServicesConfiguration, Microsoft.Web.Services2, Version=<chsdate w:st=" on isrocdate="False" islunardate="False" day="30" month="12" year="1899">2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" /><br></section></configsections>表示引用的是wse2.0
在<system.web></system.web>下加:<webservices><br><soapextensiontypes><br><add type="Microsoft.Web.Services2.WebServicesExtension, Microsoft.Web.Services2, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" priority="1" group="0"></add><br></soapextensiontypes><br></webservices>
在configuration节点下加:<microsoft.web.services2><br><security><br><x509 allowtestroot="true" allowrevocationurlretrieval="false" verifytrust="true"></x509><br></security><br></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中的代码。
在调用web service时,如果web service需要客户端证书,也就是需要走ssl协议,那么在调用的时候就需要加载上一个客户端证书,这个客户端证书是一个.cer文件,可以从浏览器的证书中导出,在导出的时候不用导出私钥,这样导出的证书是不包含私钥的,也即这个证书文件拷贝到其它机器是无效的。
在调用的时候比较简单。如下:
// The path to the certificate.
string Certificate = "Certificate.cer";
// Load the certificate into an X509Certificate object.
X509Certificate cert = X509Certificate.CreateFromCertFile(Certificate);
LoginService srv = new LoginService();
srv.ClientCertificates.Add(cert);
如果不加载这个证书就会返回403禁止访问错误。
另外为了避免每次new的时候都采用代码加载证书,可以直接修改ws的代理类,比如:
public LoginService() {
string urlSetting = System.Configuration.ConfigurationSettings.AppSettings["LoginPlugin.localhost.LoginService"];
if ((urlSetting != null)) {
this.Url = string.Concat(urlSetting, "");
}
else {
this.Url = "http://localhost/Jiancha2/Services/LoginService.asmx";
}
if (System.Configuration.ConfigurationSettings.AppSettings["ssl"] == "true" && Ocean.Plugins.CertInfo.Cert != null)
{
this.ClientCertificates.Add(Ocean.Plugins.CertInfo.Cert);
}
}
至于证书服务器和web服务器如何支持ssl,这个在dev-club的电子杂志上有一期有专门的讲解,我就不多说了。
编写者:郑昀@UltraPower 20050328
编写者:郑昀@UltraPower
20050328
使用HttpWebRequest请求远端服务器时如何加载SSL证书
编写者:郑昀@UltraPower
首先加上引用“System.Security.DLL”,
其次在工程中
using System.Security.Cryptography.X509Certificates;
这样就可以使用“
X509Certificate Class
之后我们就可以
/// 构建请求的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发布了,大家都来一起研究吧,我先来抛砖引玉:
调用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><br><section name="microsoft.web.services2" type="Microsoft.Web.Services2.Configuration.WebServicesConfiguration, Microsoft.Web.Services2, Version=<chsdate w:st=" on isrocdate="False" islunardate="False" day="30" month="12" year="1899">2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" /><br></section></configsections>表示引用的是wse2.0
在<system.web></system.web>下加:<webservices><br><soapextensiontypes><br><add type="Microsoft.Web.Services2.WebServicesExtension, Microsoft.Web.Services2, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" priority="1" group="0"></add><br></soapextensiontypes><br></webservices>
在configuration节点下加:<microsoft.web.services2><br><security><br><x509 allowtestroot="true" allowrevocationurlretrieval="false" verifytrust="true"></x509><br></security><br></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中的代码。
在调用web service时,如果web service需要客户端证书,也就是需要走ssl协议,那么在调用的时候就需要加载上一个客户端证书,这个客户端证书是一个.cer文件,可以从浏览器的证书中导出,在导出的时候不用导出私钥,这样导出的证书是不包含私钥的,也即这个证书文件拷贝到其它机器是无效的。
在调用的时候比较简单。如下:
// The path to the certificate.
string Certificate = "Certificate.cer";
// Load the certificate into an X509Certificate object.
X509Certificate cert = X509Certificate.CreateFromCertFile(Certificate);
LoginService srv = new LoginService();
srv.ClientCertificates.Add(cert);
如果不加载这个证书就会返回403禁止访问错误。
另外为了避免每次new的时候都采用代码加载证书,可以直接修改ws的代理类,比如:
public LoginService() {
string urlSetting = System.Configuration.ConfigurationSettings.AppSettings["LoginPlugin.localhost.LoginService"];
if ((urlSetting != null)) {
this.Url = string.Concat(urlSetting, "");
}
else {
this.Url = "http://localhost/Jiancha2/Services/LoginService.asmx";
}
if (System.Configuration.ConfigurationSettings.AppSettings["ssl"] == "true" && Ocean.Plugins.CertInfo.Cert != null)
{
this.ClientCertificates.Add(Ocean.Plugins.CertInfo.Cert);
}
}
至于证书服务器和web服务器如何支持ssl,这个在dev-club的电子杂志上有一期有专门的讲解,我就不多说了。
编写者:郑昀@UltraPower 20050328
编写者:郑昀@UltraPower
20050328
使用HttpWebRequest请求远端服务器时如何加载SSL证书
编写者:郑昀@UltraPower
首先加上引用“System.Security.DLL”,
其次在工程中
using System.Security.Cryptography.X509Certificates;
这样就可以使用“
X509Certificate Class
之后我们就可以
/// 构建请求的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发布了,大家都来一起研究吧,我先来抛砖引玉:
调用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><br><section name="microsoft.web.services2" type="Microsoft.Web.Services2.Configuration.WebServicesConfiguration, Microsoft.Web.Services2, Version=<chsdate w:st=" on isrocdate="False" islunardate="False" day="30" month="12" year="1899">2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" /><br></section></configsections>表示引用的是wse2.0
在<system.web></system.web>下加:<webservices><br><soapextensiontypes><br><add type="Microsoft.Web.Services2.WebServicesExtension, Microsoft.Web.Services2, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" priority="1" group="0"></add><br></soapextensiontypes><br></webservices>
在configuration节点下加:<microsoft.web.services2><br><security><br><x509 allowtestroot="true" allowrevocationurlretrieval="false" verifytrust="true"></x509><br></security><br></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中的代码。
在调用web service时,如果web service需要客户端证书,也就是需要走ssl协议,那么在调用的时候就需要加载上一个客户端证书,这个客户端证书是一个.cer文件,可以从浏览器的证书中导出,在导出的时候不用导出私钥,这样导出的证书是不包含私钥的,也即这个证书文件拷贝到其它机器是无效的。
在调用的时候比较简单。如下:
// The path to the certificate.
string Certificate = "Certificate.cer";
// Load the certificate into an X509Certificate object.
X509Certificate cert = X509Certificate.CreateFromCertFile(Certificate);
LoginService srv = new LoginService();
srv.ClientCertificates.Add(cert);
如果不加载这个证书就会返回403禁止访问错误。
另外为了避免每次new的时候都采用代码加载证书,可以直接修改ws的代理类,比如:
public LoginService() {
string urlSetting = System.Configuration.ConfigurationSettings.AppSettings["LoginPlugin.localhost.LoginService"];
if ((urlSetting != null)) {
this.Url = string.Concat(urlSetting, "");
}
else {
this.Url = "http://localhost/Jiancha2/Services/LoginService.asmx";
}
if (System.Configuration.ConfigurationSettings.AppSettings["ssl"] == "true" && Ocean.Plugins.CertInfo.Cert != null)
{
this.ClientCertificates.Add(Ocean.Plugins.CertInfo.Cert);
}
}
至于证书服务器和web服务器如何支持ssl,这个在dev-club的电子杂志上有一期有专门的讲解,我就不多说了。
编写者:郑昀@UltraPower 20050328
编写者:郑昀@UltraPower
20050328
使用HttpWebRequest请求远端服务器时如何加载SSL证书
编写者:郑昀@UltraPower
首先加上引用“System.Security.DLL”,
其次在工程中
using System.Security.Cryptography.X509Certificates;
这样就可以使用“
X509Certificate Class
之后我们就可以
/// 构建请求的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发布了,大家都来一起研究吧,我先来抛砖引玉:
调用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><br><section name="microsoft.web.services2" type="Microsoft.Web.Services2.Configuration.WebServicesConfiguration, Microsoft.Web.Services2, Version=<chsdate w:st=" on isrocdate="False" islunardate="False" day="30" month="12" year="1899">2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" /><br></section></configsections>表示引用的是wse2.0
在<system.web></system.web>下加:<webservices><br><soapextensiontypes><br><add type="Microsoft.Web.Services2.WebServicesExtension, Microsoft.Web.Services2, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" priority="1" group="0"></add><br></soapextensiontypes><br></webservices>
在configuration节点下加:<microsoft.web.services2><br><security><br><x509 allowtestroot="true" allowrevocationurlretrieval="false" verifytrust="true"></x509><br></security><br></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中的代码。
在调用web service时,如果web service需要客户端证书,也就是需要走ssl协议,那么在调用的时候就需要加载上一个客户端证书,这个客户端证书是一个.cer文件,可以从浏览器的证书中导出,在导出的时候不用导出私钥,这样导出的证书是不包含私钥的,也即这个证书文件拷贝到其它机器是无效的。
在调用的时候比较简单。如下:
// The path to the certificate.
string Certificate = "Certificate.cer";
// Load the certificate into an X509Certificate object.
X509Certificate cert = X509Certificate.CreateFromCertFile(Certificate);
LoginService srv = new LoginService();
srv.ClientCertificates.Add(cert);
如果不加载这个证书就会返回403禁止访问错误。
另外为了避免每次new的时候都采用代码加载证书,可以直接修改ws的代理类,比如:
public LoginService() {
string urlSetting = System.Configuration.ConfigurationSettings.AppSettings["LoginPlugin.localhost.LoginService"];
if ((urlSetting != null)) {
this.Url = string.Concat(urlSetting, "");
}
else {
this.Url = "http://localhost/Jiancha2/Services/LoginService.asmx";
}
if (System.Configuration.ConfigurationSettings.AppSettings["ssl"] == "true" && Ocean.Plugins.CertInfo.Cert != null)
{
this.ClientCertificates.Add(Ocean.Plugins.CertInfo.Cert);
}
}
至于证书服务器和web服务器如何支持ssl,这个在dev-club的电子杂志上有一期有专门的讲解,我就不多说了。
编写者:郑昀@UltraPower 20050328
编写者:郑昀@UltraPower
20050328
使用HttpWebRequest请求远端服务器时如何加载SSL证书
编写者:郑昀@UltraPower
首先加上引用“System.Security.DLL”,
其次在工程中
using System.Security.Cryptography.X509Certificates;
这样就可以使用“
X509Certificate Class
之后我们就可以
/// 构建请求的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发布了,大家都来一起研究吧,我先来抛砖引玉:
调用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><br><section name="microsoft.web.services2" type="Microsoft.Web.Services2.Configuration.WebServicesConfiguration, Microsoft.Web.Services2, Version=<chsdate w:st=" on isrocdate="False" islunardate="False" day="30" month="12" year="1899">2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" /><br></section></configsections>表示引用的是wse2.0
在<system.web></system.web>下加:<webservices><br><soapextensiontypes><br><add type="Microsoft.Web.Services2.WebServicesExtension, Microsoft.Web.Services2, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" priority="1" group="0"></add><br></soapextensiontypes><br></webservices>
在configuration节点下加:<microsoft.web.services2><br><security><br><x509 allowtestroot="true" allowrevocationurlretrieval="false" verifytrust="true"></x509><br></security><br></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中的代码。
在调用web service时,如果web service需要客户端证书,也就是需要走ssl协议,那么在调用的时候就需要加载上一个客户端证书,这个客户端证书是一个.cer文件,可以从浏览器的证书中导出,在导出的时候不用导出私钥,这样导出的证书是不包含私钥的,也即这个证书文件拷贝到其它机器是无效的。
在调用的时候比较简单。如下:
// The path to the certificate.
string Certificate = "Certificate.cer";
// Load the certificate into an X509Certificate object.
X509Certificate cert = X509Certificate.CreateFromCertFile(Certificate);
LoginService srv = new LoginService();
srv.ClientCertificates.Add(cert);
如果不加载这个证书就会返回403禁止访问错误。
另外为了避免每次new的时候都采用代码加载证书,可以直接修改ws的代理类,比如:
public LoginService() {
string urlSetting = System.Configuration.ConfigurationSettings.AppSettings["LoginPlugin.localhost.LoginService"];
if ((urlSetting != null)) {
this.Url = string.Concat(urlSetting, "");
}
else {
this.Url = "http://localhost/Jiancha2/Services/LoginService.asmx";
}
if (System.Configuration.ConfigurationSettings.AppSettings["ssl"] == "true" && Ocean.Plugins.CertInfo.Cert != null)
{
this.ClientCertificates.Add(Ocean.Plugins.CertInfo.Cert);
}
}
至于证书服务器和web服务器如何支持ssl,这个在dev-club的电子杂志上有一期有专门的讲解,我就不多说了。
编写者:郑昀@UltraPower 20050328
编写者:郑昀@UltraPower
20050328
使用HttpWebRequest请求远端服务器时如何加载SSL证书
编写者:郑昀@UltraPower
首先加上引用“System.Security.DLL”,
其次在工程中
using System.Security.Cryptography.X509Certificates;
这样就可以使用“
X509Certificate Class
之后我们就可以
/// 构建请求的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发布了,大家都来一起研究吧,我先来抛砖引玉:
调用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><br><section name="microsoft.web.services2" type="Microsoft.Web.Services2.Configuration.WebServicesConfiguration, Microsoft.Web.Services2, Version=<chsdate w:st=" on isrocdate="False" islunardate="False" day="30" month="12" year="1899">2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" /><br></section></configsections>表示引用的是wse2.0
在<system.web></system.web>下加:<webservices><br><soapextensiontypes><br><add type="Microsoft.Web.Services2.WebServicesExtension, Microsoft.Web.Services2, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" priority="1" group="0"></add><br></soapextensiontypes><br></webservices>
在configuration节点下加:<microsoft.web.services2><br><security><br><x509 allowtestroot="true" allowrevocationurlretrieval="false" verifytrust="true"></x509><br></security><br></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中的代码。
在调用web service时,如果web service需要客户端证书,也就是需要走ssl协议,那么在调用的时候就需要加载上一个客户端证书,这个客户端证书是一个.cer文件,可以从浏览器的证书中导出,在导出的时候不用导出私钥,这样导出的证书是不包含私钥的,也即这个证书文件拷贝到其它机器是无效的。
在调用的时候比较简单。如下:
// The path to the certificate.
string Certificate = "Certificate.cer";
// Load the certificate into an X509Certificate object.
X509Certificate cert = X509Certificate.CreateFromCertFile(Certificate);
LoginService srv = new LoginService();
srv.ClientCertificates.Add(cert);
如果不加载这个证书就会返回403禁止访问错误。
另外为了避免每次new的时候都采用代码加载证书,可以直接修改ws的代理类,比如:
public LoginService() {
string urlSetting = System.Configuration.ConfigurationSettings.AppSettings["LoginPlugin.localhost.LoginService"];
if ((urlSetting != null)) {
this.Url = string.Concat(urlSetting, "");
}
else {
this.Url = "http://localhost/Jiancha2/Services/LoginService.asmx";
}
if (System.Configuration.ConfigurationSettings.AppSettings["ssl"] == "true" && Ocean.Plugins.CertInfo.Cert != null)
{
this.ClientCertificates.Add(Ocean.Plugins.CertInfo.Cert);
}
}
至于证书服务器和web服务器如何支持ssl,这个在dev-club的电子杂志上有一期有专门的讲解,我就不多说了。
编写者:郑昀@UltraPower 20050328
编写者:郑昀@UltraPower
20050328
使用HttpWebRequest请求远端服务器时如何加载SSL证书
编写者:郑昀@UltraPower
首先加上引用“System.Security.DLL”,
其次在工程中
using System.Security.Cryptography.X509Certificates;
这样就可以使用“
X509Certificate Class
之后我们就可以
/// 构建请求的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发布了,大家都来一起研究吧,我先来抛砖引玉:
调用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><br><section name="microsoft.web.services2" type="Microsoft.Web.Services2.Configuration.WebServicesConfiguration, Microsoft.Web.Services2, Version=<chsdate w:st=" on isrocdate="False" islunardate="False" day="30" month="12" year="1899">2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" /><br></section></configsections>表示引用的是wse2.0
在<system.web></system.web>下加:<webservices><br><soapextensiontypes><br><add type="Microsoft.Web.Services2.WebServicesExtension, Microsoft.Web.Services2, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" priority="1" group="0"></add><br></soapextensiontypes><br></webservices>
在configuration节点下加:<microsoft.web.services2><br><security><br><x509 allowtestroot="true" allowrevocationurlretrieval="false" verifytrust="true"></x509><br></security><br></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中的代码。
在调用web service时,如果web service需要客户端证书,也就是需要走ssl协议,那么在调用的时候就需要加载上一个客户端证书,这个客户端证书是一个.cer文件,可以从浏览器的证书中导出,在导出的时候不用导出私钥,这样导出的证书是不包含私钥的,也即这个证书文件拷贝到其它机器是无效的。
在调用的时候比较简单。如下:
// The path to the certificate.
string Certificate = "Certificate.cer";
// Load the certificate into an X509Certificate object.
X509Certificate cert = X509Certificate.CreateFromCertFile(Certificate);
LoginService srv = new LoginService();
srv.ClientCertificates.Add(cert);
如果不加载这个证书就会返回403禁止访问错误。
另外为了避免每次new的时候都采用代码加载证书,可以直接修改ws的代理类,比如:
public LoginService() {
string urlSetting = System.Configuration.ConfigurationSettings.AppSettings["LoginPlugin.localhost.LoginService"];
if ((urlSetting != null)) {
this.Url = string.Concat(urlSetting, "");
}
else {
this.Url = "http://localhost/Jiancha2/Services/LoginService.asmx";
}
if (System.Configuration.ConfigurationSettings.AppSettings["ssl"] == "true" && Ocean.Plugins.CertInfo.Cert != null)
{
this.ClientCertificates.Add(Ocean.Plugins.CertInfo.Cert);
}
}
至于证书服务器和web服务器如何支持ssl,这个在dev-club的电子杂志上有一期有专门的讲解,我就不多说了。
编写者:郑昀@UltraPower 20050328
编写者:郑昀@UltraPower
20050328
使用HttpWebRequest请求远端服务器时如何加载SSL证书
编写者:郑昀@UltraPower
首先加上引用“System.Security.DLL”,
其次在工程中
using System.Security.Cryptography.X509Certificates;
这样就可以使用“
X509Certificate Class
之后我们就可以
/// 构建请求的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发布了,大家都来一起研究吧,我先来抛砖引玉:
调用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><br><section name="microsoft.web.services2" type="Microsoft.Web.Services2.Configuration.WebServicesConfiguration, Microsoft.Web.Services2, Version=<chsdate w:st=" on isrocdate="False" islunardate="False" day="30" month="12" year="1899">2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" /><br></section></configsections>表示引用的是wse2.0
在<system.web></system.web>下加:<webservices><br><soapextensiontypes><br><add type="Microsoft.Web.Services2.WebServicesExtension, Microsoft.Web.Services2, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" priority="1" group="0"></add><br></soapextensiontypes><br></webservices>
在configuration节点下加:<microsoft.web.services2><br><security><br><x509 allowtestroot="true" allowrevocationurlretrieval="false" verifytrust="true"></x509><br></security><br></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中的代码。
在调用web service时,如果web service需要客户端证书,也就是需要走ssl协议,那么在调用的时候就需要加载上一个客户端证书,这个客户端证书是一个.cer文件,可以从浏览器的证书中导出,在导出的时候不用导出私钥,这样导出的证书是不包含私钥的,也即这个证书文件拷贝到其它机器是无效的。
在调用的时候比较简单。如下:
// The path to the certificate.
string Certificate = "Certificate.cer";
// Load the certificate into an X509Certificate object.
X509Certificate cert = X509Certificate.CreateFromCertFile(Certificate);
LoginService srv = new LoginService();
srv.ClientCertificates.Add(cert);
如果不加载这个证书就会返回403禁止访问错误。
另外为了避免每次new的时候都采用代码加载证书,可以直接修改ws的代理类,比如:
public LoginService() {
string urlSetting = System.Configuration.ConfigurationSettings.AppSettings["LoginPlugin.localhost.LoginService"];
if ((urlSetting != null)) {
this.Url = string.Concat(urlSetting, "");
}
else {
this.Url = "http://localhost/Jiancha2/Services/LoginService.asmx";
}
if (System.Configuration.ConfigurationSettings.AppSettings["ssl"] == "true" && Ocean.Plugins.CertInfo.Cert != null)
{
this.ClientCertificates.Add(Ocean.Plugins.CertInfo.Cert);
}
}
至于证书服务器和web服务器如何支持ssl,这个在dev-club的电子杂志上有一期有专门的讲解,我就不多说了。
编写者:郑昀@UltraPower 20050328
编写者:郑昀@UltraPower
20050328
相关推荐
配置dotnet+nginx+SSL+centos
不错的工具。需要dotnet 1.0。可以检测你的服务器支持什么加密传输。
dotnet-DotNETCore实现微信公众号被关注时推送消息dotnet-DotNETCore实现微信公众号被关注时推送消息dotnet-DotNETCore实现微信公众号被关注时推送消息dotnet-DotNETCore实现微信公众号被关注时推送消息dotnet-...
本文告诉大家如何在 UWP 做客户端和 asp dotnet core 做服务器端
IIS 发布 Core Web 项目,发布后提示没有文件AspNetCoreModuleV2,这个时候需要在机器上安装dotnet-hosting、dotnet-sdk 以上两个文件时64位的。
In Memory Powershell WebDav Server
Vlc.DotNet-develop.zip 二次开发包SDK 请使用VS2017运行 Vlc.DotNet-develop.zip 二次开发包SDK 请使用VS2017运行 Vlc.DotNet-develop.zip 二次开发包SDK 请使用VS2017运行 Vlc.DotNet-develop.zip 二次开发包SDK ...
仅示例,button3有点问题 用于提问
DotNet第三方控件使用笔记DotNet第三方控件使用笔记DotNet第三方控件使用笔记
本软件是一个VS编译工具,是目前体积最小的VS编译器,支持c#、vb.net,支持winform和webform语言虽不能完全替代Visual studio,不过在没有Visual studio时,应急用下,还是不错的,此为dotNet1.1环境。 ------------...
提供了一种为特定主机颁发SSL证书的简便方法。 包含用于从PFX到PFX格式或从PFX到PEM格式的转换工具(PKCS12) 快速开始 要求 .NET Core 3.1 启动证书颁发机构dotnet TestAuthority.dll 在Docker容器中启动项目 ...
dotnet 相关命令是属于 .NET Core command-line (CLI) 的一部分,Microsoft 为我们提供了这个命令行工具以供我们在开发程序中使用,它主要用来进行对代码的编译、NuGet 包的管理、程序的运行、测试等等。
一个用于记录和重放HTTP请求以进行测试的简单方法
dotNET下开发操作WORD时使用的WORD控件 本人收藏了3年的资源 现放出 都是总结了很多系统 软件项目实施过程中的经验的 慢慢积累的
DotNet第三方控件使用笔记.doc
Dotnet4.8高度兼容版,内含开发环境和运行环境脱机安装。
dotNET Reactor 使用步骤图解
有了稳定的装配读者,的DotNet解析器不会遇到无效的结构在应用程序失败时那么快。有了这个功能,你可以打开被保护与谁已申请无效的元数据混淆或提出的PE结构无效混淆器的应用程序。 会员分析仪 成员分析仪,可以...
基于DotNetCore的gRPC 通信示例代码,有了 gRPC, 我们可以一次性的在一个 .proto 文件中定义服务并使用任何支持它的语言去实现客户端和服务器,反过来,它们可以在各种环境中,从Google的服务器到你自己的平板电脑-...
1.使用dotnet生成dll文件新建一个文件夹,在该文件夹目录下打开 powershell(shift + 鼠标右键)2对dll文件进行编译3设置环境变量变量