[dotNET]使用HttpWebRequest请求远端服务器时如何加载SSL证书

news/2024/7/1 21:14:20

使用HttpWebRequest请求远端服务器时如何加载SSL证书

编写者:郑昀@UltraPower

 

首先加上引用“System.Security.DLL”

其次在工程中

using System.Security.Cryptography.X509Certificates;

这样就可以使用“

X509Certificate Class

”了,它的定义参见http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemsecuritycryptographyx509certificatesx509certificateclasstopic.asp

之后我们就可以

/// 构建请求的HttpWebRequest对象

HttpWebRequest hwrRequest = (HttpWebRequest)WebRequest.Create(

                                   strValidatePageURL);

/// 从本地文件中加载证书

hwrRequest.ClientCertificates.Add(X509Certificate.CreateFromCertFile("c://motor.https.pem.cer"));

 

这是一个较简单的办法。

如果你遇到了“The underlying connection was closed. Could not establish a secure SSL/TLS connection"”的异常,那么请设置

hwrRequest.KeepAlive = true;

 

如果您使用的是CreateFromSignedFile来创建证书,那么请您务必注意,即使CreateFromSignedFile没有能够从文件中创建证书,甚至即使没有找到该文件,他也不会抛出异常,也不返回null,只是他的各个字段为null

所以,。。。,还是请使用CreateFromCertFile好了。

 

至于如何在个人证书存储区获取证书,参看下面的blog

参看:

WSE2.0X509安全令牌的使用

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节点下加:
   

2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
  表示引用的是 wse2.0
下加:
     
       
     
   
configuration 节点下加:

     
   
 
这个 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时,如果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

”了,它的定义参见http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemsecuritycryptographyx509certificatesx509certificateclasstopic.asp

之后我们就可以

/// 构建请求的HttpWebRequest对象

HttpWebRequest hwrRequest = (HttpWebRequest)WebRequest.Create(

                                   strValidatePageURL);

/// 从本地文件中加载证书

hwrRequest.ClientCertificates.Add(X509Certificate.CreateFromCertFile("c://motor.https.pem.cer"));

 

这是一个较简单的办法。

如果你遇到了“The underlying connection was closed. Could not establish a secure SSL/TLS connection"”的异常,那么请设置

hwrRequest.KeepAlive = true;

 

如果您使用的是CreateFromSignedFile来创建证书,那么请您务必注意,即使CreateFromSignedFile没有能够从文件中创建证书,甚至即使没有找到该文件,他也不会抛出异常,也不返回null,只是他的各个字段为null

所以,。。。,还是请使用CreateFromCertFile好了。

 

至于如何在个人证书存储区获取证书,参看下面的blog

参看:

WSE2.0X509安全令牌的使用

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节点下加:
   

2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
  表示引用的是 wse2.0
下加:
     
       
     
   
configuration 节点下加:

     
   
 
这个 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时,如果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

”了,它的定义参见http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemsecuritycryptographyx509certificatesx509certificateclasstopic.asp

之后我们就可以

/// 构建请求的HttpWebRequest对象

HttpWebRequest hwrRequest = (HttpWebRequest)WebRequest.Create(

                                   strValidatePageURL);

/// 从本地文件中加载证书

hwrRequest.ClientCertificates.Add(X509Certificate.CreateFromCertFile("c://motor.https.pem.cer"));

 

这是一个较简单的办法。

如果你遇到了“The underlying connection was closed. Could not establish a secure SSL/TLS connection"”的异常,那么请设置

hwrRequest.KeepAlive = true;

 

如果您使用的是CreateFromSignedFile来创建证书,那么请您务必注意,即使CreateFromSignedFile没有能够从文件中创建证书,甚至即使没有找到该文件,他也不会抛出异常,也不返回null,只是他的各个字段为null

所以,。。。,还是请使用CreateFromCertFile好了。

 

至于如何在个人证书存储区获取证书,参看下面的blog

参看:

WSE2.0X509安全令牌的使用

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节点下加:
   

2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
  表示引用的是 wse2.0
下加:
     
       
     
   
configuration 节点下加:

     
   
 
这个 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时,如果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

”了,它的定义参见http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemsecuritycryptographyx509certificatesx509certificateclasstopic.asp

之后我们就可以

/// 构建请求的HttpWebRequest对象

HttpWebRequest hwrRequest = (HttpWebRequest)WebRequest.Create(

                                   strValidatePageURL);

/// 从本地文件中加载证书

hwrRequest.ClientCertificates.Add(X509Certificate.CreateFromCertFile("c://motor.https.pem.cer"));

 

这是一个较简单的办法。

如果你遇到了“The underlying connection was closed. Could not establish a secure SSL/TLS connection"”的异常,那么请设置

hwrRequest.KeepAlive = true;

 

如果您使用的是CreateFromSignedFile来创建证书,那么请您务必注意,即使CreateFromSignedFile没有能够从文件中创建证书,甚至即使没有找到该文件,他也不会抛出异常,也不返回null,只是他的各个字段为null

所以,。。。,还是请使用CreateFromCertFile好了。

 

至于如何在个人证书存储区获取证书,参看下面的blog

参看:

WSE2.0X509安全令牌的使用

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节点下加:
   

2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
  表示引用的是 wse2.0
下加:
     
       
     
   
configuration 节点下加:

     
   
 
这个 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时,如果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

”了,它的定义参见http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemsecuritycryptographyx509certificatesx509certificateclasstopic.asp

之后我们就可以

/// 构建请求的HttpWebRequest对象

HttpWebRequest hwrRequest = (HttpWebRequest)WebRequest.Create(

                                   strValidatePageURL);

/// 从本地文件中加载证书

hwrRequest.ClientCertificates.Add(X509Certificate.CreateFromCertFile("c://motor.https.pem.cer"));

 

这是一个较简单的办法。

如果你遇到了“The underlying connection was closed. Could not establish a secure SSL/TLS connection"”的异常,那么请设置

hwrRequest.KeepAlive = true;

 

如果您使用的是CreateFromSignedFile来创建证书,那么请您务必注意,即使CreateFromSignedFile没有能够从文件中创建证书,甚至即使没有找到该文件,他也不会抛出异常,也不返回null,只是他的各个字段为null

所以,。。。,还是请使用CreateFromCertFile好了。

 

至于如何在个人证书存储区获取证书,参看下面的blog

参看:

WSE2.0X509安全令牌的使用

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节点下加:
   

2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
  表示引用的是 wse2.0
下加:
     
       
     
   
configuration 节点下加:

     
   
 
这个 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时,如果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

”了,它的定义参见http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemsecuritycryptographyx509certificatesx509certificateclasstopic.asp

之后我们就可以

/// 构建请求的HttpWebRequest对象

HttpWebRequest hwrRequest = (HttpWebRequest)WebRequest.Create(

                                   strValidatePageURL);

/// 从本地文件中加载证书

hwrRequest.ClientCertificates.Add(X509Certificate.CreateFromCertFile("c://motor.https.pem.cer"));

 

这是一个较简单的办法。

如果你遇到了“The underlying connection was closed. Could not establish a secure SSL/TLS connection"”的异常,那么请设置

hwrRequest.KeepAlive = true;

 

如果您使用的是CreateFromSignedFile来创建证书,那么请您务必注意,即使CreateFromSignedFile没有能够从文件中创建证书,甚至即使没有找到该文件,他也不会抛出异常,也不返回null,只是他的各个字段为null

所以,。。。,还是请使用CreateFromCertFile好了。

 

至于如何在个人证书存储区获取证书,参看下面的blog

参看:

WSE2.0X509安全令牌的使用

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节点下加:
   

2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
  表示引用的是 wse2.0
下加:
     
       
     
   
configuration 节点下加:

     
   
 
这个 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时,如果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

”了,它的定义参见http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemsecuritycryptographyx509certificatesx509certificateclasstopic.asp

之后我们就可以

/// 构建请求的HttpWebRequest对象

HttpWebRequest hwrRequest = (HttpWebRequest)WebRequest.Create(

                                   strValidatePageURL);

/// 从本地文件中加载证书

hwrRequest.ClientCertificates.Add(X509Certificate.CreateFromCertFile("c://motor.https.pem.cer"));

 

这是一个较简单的办法。

如果你遇到了“The underlying connection was closed. Could not establish a secure SSL/TLS connection"”的异常,那么请设置

hwrRequest.KeepAlive = true;

 

如果您使用的是CreateFromSignedFile来创建证书,那么请您务必注意,即使CreateFromSignedFile没有能够从文件中创建证书,甚至即使没有找到该文件,他也不会抛出异常,也不返回null,只是他的各个字段为null

所以,。。。,还是请使用CreateFromCertFile好了。

 

至于如何在个人证书存储区获取证书,参看下面的blog

参看:

WSE2.0X509安全令牌的使用

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节点下加:
   

2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
  表示引用的是 wse2.0
下加:
     
       
     
   
configuration 节点下加:

     
   
 
这个 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时,如果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

”了,它的定义参见http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemsecuritycryptographyx509certificatesx509certificateclasstopic.asp

之后我们就可以

/// 构建请求的HttpWebRequest对象

HttpWebRequest hwrRequest = (HttpWebRequest)WebRequest.Create(

                                   strValidatePageURL);

/// 从本地文件中加载证书

hwrRequest.ClientCertificates.Add(X509Certificate.CreateFromCertFile("c://motor.https.pem.cer"));

 

这是一个较简单的办法。

如果你遇到了“The underlying connection was closed. Could not establish a secure SSL/TLS connection"”的异常,那么请设置

hwrRequest.KeepAlive = true;

 

如果您使用的是CreateFromSignedFile来创建证书,那么请您务必注意,即使CreateFromSignedFile没有能够从文件中创建证书,甚至即使没有找到该文件,他也不会抛出异常,也不返回null,只是他的各个字段为null

所以,。。。,还是请使用CreateFromCertFile好了。

 

至于如何在个人证书存储区获取证书,参看下面的blog

参看:

WSE2.0X509安全令牌的使用

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节点下加:
   

2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
  表示引用的是 wse2.0
下加:
     
       
     
   
configuration 节点下加:

     
   
 
这个 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时,如果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

”了,它的定义参见http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemsecuritycryptographyx509certificatesx509certificateclasstopic.asp

之后我们就可以

/// 构建请求的HttpWebRequest对象

HttpWebRequest hwrRequest = (HttpWebRequest)WebRequest.Create(

                                   strValidatePageURL);

/// 从本地文件中加载证书

hwrRequest.ClientCertificates.Add(X509Certificate.CreateFromCertFile("c://motor.https.pem.cer"));

 

这是一个较简单的办法。

如果你遇到了“The underlying connection was closed. Could not establish a secure SSL/TLS connection"”的异常,那么请设置

hwrRequest.KeepAlive = true;

 

如果您使用的是CreateFromSignedFile来创建证书,那么请您务必注意,即使CreateFromSignedFile没有能够从文件中创建证书,甚至即使没有找到该文件,他也不会抛出异常,也不返回null,只是他的各个字段为null

所以,。。。,还是请使用CreateFromCertFile好了。

 

至于如何在个人证书存储区获取证书,参看下面的blog

参看:

WSE2.0X509安全令牌的使用

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节点下加:
   

2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
  表示引用的是 wse2.0
下加:
     
       
     
   
configuration 节点下加:

     
   
 
这个 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时,如果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

”了,它的定义参见http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemsecuritycryptographyx509certificatesx509certificateclasstopic.asp

之后我们就可以

/// 构建请求的HttpWebRequest对象

HttpWebRequest hwrRequest = (HttpWebRequest)WebRequest.Create(

                                   strValidatePageURL);

/// 从本地文件中加载证书

hwrRequest.ClientCertificates.Add(X509Certificate.CreateFromCertFile("c://motor.https.pem.cer"));

 

这是一个较简单的办法。

如果你遇到了“The underlying connection was closed. Could not establish a secure SSL/TLS connection"”的异常,那么请设置

hwrRequest.KeepAlive = true;

 

如果您使用的是CreateFromSignedFile来创建证书,那么请您务必注意,即使CreateFromSignedFile没有能够从文件中创建证书,甚至即使没有找到该文件,他也不会抛出异常,也不返回null,只是他的各个字段为null

所以,。。。,还是请使用CreateFromCertFile好了。

 

至于如何在个人证书存储区获取证书,参看下面的blog

参看:

WSE2.0X509安全令牌的使用

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节点下加:
   

2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
  表示引用的是 wse2.0
下加:
     
       
     
   
configuration 节点下加:

     
   
 
这个 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时,如果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

”了,它的定义参见http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemsecuritycryptographyx509certificatesx509certificateclasstopic.asp

之后我们就可以

/// 构建请求的HttpWebRequest对象

HttpWebRequest hwrRequest = (HttpWebRequest)WebRequest.Create(

                                   strValidatePageURL);

/// 从本地文件中加载证书

hwrRequest.ClientCertificates.Add(X509Certificate.CreateFromCertFile("c://motor.https.pem.cer"));

 

这是一个较简单的办法。

如果你遇到了“The underlying connection was closed. Could not establish a secure SSL/TLS connection"”的异常,那么请设置

hwrRequest.KeepAlive = true;

 

如果您使用的是CreateFromSignedFile来创建证书,那么请您务必注意,即使CreateFromSignedFile没有能够从文件中创建证书,甚至即使没有找到该文件,他也不会抛出异常,也不返回null,只是他的各个字段为null

所以,。。。,还是请使用CreateFromCertFile好了。

 

至于如何在个人证书存储区获取证书,参看下面的blog

参看:

WSE2.0X509安全令牌的使用

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节点下加:
   

2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
  表示引用的是 wse2.0
下加:
     
       
     
   
configuration 节点下加:

     
   
 
这个 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时,如果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

”了,它的定义参见http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemsecuritycryptographyx509certificatesx509certificateclasstopic.asp

之后我们就可以

/// 构建请求的HttpWebRequest对象

HttpWebRequest hwrRequest = (HttpWebRequest)WebRequest.Create(

                                   strValidatePageURL);

/// 从本地文件中加载证书

hwrRequest.ClientCertificates.Add(X509Certificate.CreateFromCertFile("c://motor.https.pem.cer"));

 

这是一个较简单的办法。

如果你遇到了“The underlying connection was closed. Could not establish a secure SSL/TLS connection"”的异常,那么请设置

hwrRequest.KeepAlive = true;

 

如果您使用的是CreateFromSignedFile来创建证书,那么请您务必注意,即使CreateFromSignedFile没有能够从文件中创建证书,甚至即使没有找到该文件,他也不会抛出异常,也不返回null,只是他的各个字段为null

所以,。。。,还是请使用CreateFromCertFile好了。

 

至于如何在个人证书存储区获取证书,参看下面的blog

参看:

WSE2.0X509安全令牌的使用

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节点下加:
   

2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
  表示引用的是 wse2.0
下加:
     
       
     
   
configuration 节点下加:

     
   
 
这个 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时,如果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

”了,它的定义参见http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemsecuritycryptographyx509certificatesx509certificateclasstopic.asp

之后我们就可以

/// 构建请求的HttpWebRequest对象

HttpWebRequest hwrRequest = (HttpWebRequest)WebRequest.Create(

                                   strValidatePageURL);

/// 从本地文件中加载证书

hwrRequest.ClientCertificates.Add(X509Certificate.CreateFromCertFile("c://motor.https.pem.cer"));

 

这是一个较简单的办法。

如果你遇到了“The underlying connection was closed. Could not establish a secure SSL/TLS connection"”的异常,那么请设置

hwrRequest.KeepAlive = true;

 

如果您使用的是CreateFromSignedFile来创建证书,那么请您务必注意,即使CreateFromSignedFile没有能够从文件中创建证书,甚至即使没有找到该文件,他也不会抛出异常,也不返回null,只是他的各个字段为null

所以,。。。,还是请使用CreateFromCertFile好了。

 

至于如何在个人证书存储区获取证书,参看下面的blog

参看:

WSE2.0X509安全令牌的使用

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节点下加:
   

2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
  表示引用的是 wse2.0
下加:
     
       
     
   
configuration 节点下加:

     
   
 
这个 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时,如果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

”了,它的定义参见http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemsecuritycryptographyx509certificatesx509certificateclasstopic.asp

之后我们就可以

/// 构建请求的HttpWebRequest对象

HttpWebRequest hwrRequest = (HttpWebRequest)WebRequest.Create(

                                   strValidatePageURL);

/// 从本地文件中加载证书

hwrRequest.ClientCertificates.Add(X509Certificate.CreateFromCertFile("c://motor.https.pem.cer"));

 

这是一个较简单的办法。

如果你遇到了“The underlying connection was closed. Could not establish a secure SSL/TLS connection"”的异常,那么请设置

hwrRequest.KeepAlive = true;

 

如果您使用的是CreateFromSignedFile来创建证书,那么请您务必注意,即使CreateFromSignedFile没有能够从文件中创建证书,甚至即使没有找到该文件,他也不会抛出异常,也不返回null,只是他的各个字段为null

所以,。。。,还是请使用CreateFromCertFile好了。

 

至于如何在个人证书存储区获取证书,参看下面的blog

参看:

WSE2.0X509安全令牌的使用

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节点下加:
   

2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
  表示引用的是 wse2.0
下加:
     
       
     
   
configuration 节点下加:

     
   
 
这个 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时,如果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

”了,它的定义参见http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemsecuritycryptographyx509certificatesx509certificateclasstopic.asp

之后我们就可以

/// 构建请求的HttpWebRequest对象

HttpWebRequest hwrRequest = (HttpWebRequest)WebRequest.Create(

                                   strValidatePageURL);

/// 从本地文件中加载证书

hwrRequest.ClientCertificates.Add(X509Certificate.CreateFromCertFile("c://motor.https.pem.cer"));

 

这是一个较简单的办法。

如果你遇到了“The underlying connection was closed. Could not establish a secure SSL/TLS connection"”的异常,那么请设置

hwrRequest.KeepAlive = true;

 

如果您使用的是CreateFromSignedFile来创建证书,那么请您务必注意,即使CreateFromSignedFile没有能够从文件中创建证书,甚至即使没有找到该文件,他也不会抛出异常,也不返回null,只是他的各个字段为null

所以,。。。,还是请使用CreateFromCertFile好了。

 

至于如何在个人证书存储区获取证书,参看下面的blog

参看:

WSE2.0X509安全令牌的使用

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节点下加:
   

2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
  表示引用的是 wse2.0
下加:
     
       
     
   
configuration 节点下加:

     
   
 
这个 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时,如果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

”了,它的定义参见http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemsecuritycryptographyx509certificatesx509certificateclasstopic.asp

之后我们就可以

/// 构建请求的HttpWebRequest对象

HttpWebRequest hwrRequest = (HttpWebRequest)WebRequest.Create(

                                   strValidatePageURL);

/// 从本地文件中加载证书

hwrRequest.ClientCertificates.Add(X509Certificate.CreateFromCertFile("c://motor.https.pem.cer"));

 

这是一个较简单的办法。

如果你遇到了“The underlying connection was closed. Could not establish a secure SSL/TLS connection"”的异常,那么请设置

hwrRequest.KeepAlive = true;

 

如果您使用的是CreateFromSignedFile来创建证书,那么请您务必注意,即使CreateFromSignedFile没有能够从文件中创建证书,甚至即使没有找到该文件,他也不会抛出异常,也不返回null,只是他的各个字段为null

所以,。。。,还是请使用CreateFromCertFile好了。

 

至于如何在个人证书存储区获取证书,参看下面的blog

参看:

WSE2.0X509安全令牌的使用

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节点下加:
   

2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
  表示引用的是 wse2.0
下加:
     
       
     
   
configuration 节点下加:

     
   
 
这个 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时,如果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

”了,它的定义参见http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemsecuritycryptographyx509certificatesx509certificateclasstopic.asp

之后我们就可以

/// 构建请求的HttpWebRequest对象

HttpWebRequest hwrRequest = (HttpWebRequest)WebRequest.Create(

                                   strValidatePageURL);

/// 从本地文件中加载证书

hwrRequest.ClientCertificates.Add(X509Certificate.CreateFromCertFile("c://motor.https.pem.cer"));

 

这是一个较简单的办法。

如果你遇到了“The underlying connection was closed. Could not establish a secure SSL/TLS connection"”的异常,那么请设置

hwrRequest.KeepAlive = true;

 

如果您使用的是CreateFromSignedFile来创建证书,那么请您务必注意,即使CreateFromSignedFile没有能够从文件中创建证书,甚至即使没有找到该文件,他也不会抛出异常,也不返回null,只是他的各个字段为null

所以,。。。,还是请使用CreateFromCertFile好了。

 

至于如何在个人证书存储区获取证书,参看下面的blog

参看:

WSE2.0X509安全令牌的使用

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节点下加:
   

2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
  表示引用的是 wse2.0
下加:
     
       
     
   
configuration 节点下加:

     
   
 
这个 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时,如果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

”了,它的定义参见http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemsecuritycryptographyx509certificatesx509certificateclasstopic.asp

之后我们就可以

/// 构建请求的HttpWebRequest对象

HttpWebRequest hwrRequest = (HttpWebRequest)WebRequest.Create(

                                   strValidatePageURL);

/// 从本地文件中加载证书

hwrRequest.ClientCertificates.Add(X509Certificate.CreateFromCertFile("c://motor.https.pem.cer"));

 

这是一个较简单的办法。

如果你遇到了“The underlying connection was closed. Could not establish a secure SSL/TLS connection"”的异常,那么请设置

hwrRequest.KeepAlive = true;

 

如果您使用的是CreateFromSignedFile来创建证书,那么请您务必注意,即使CreateFromSignedFile没有能够从文件中创建证书,甚至即使没有找到该文件,他也不会抛出异常,也不返回null,只是他的各个字段为null

所以,。。。,还是请使用CreateFromCertFile好了。

 

至于如何在个人证书存储区获取证书,参看下面的blog

参看:

WSE2.0X509安全令牌的使用

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节点下加:
   

2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
  表示引用的是 wse2.0
下加:
     
       
     
   
configuration 节点下加:

     
   
 
这个 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时,如果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

”了,它的定义参见http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemsecuritycryptographyx509certificatesx509certificateclasstopic.asp

之后我们就可以

/// 构建请求的HttpWebRequest对象

HttpWebRequest hwrRequest = (HttpWebRequest)WebRequest.Create(

                                   strValidatePageURL);

/// 从本地文件中加载证书

hwrRequest.ClientCertificates.Add(X509Certificate.CreateFromCertFile("c://motor.https.pem.cer"));

 

这是一个较简单的办法。

如果你遇到了“The underlying connection was closed. Could not establish a secure SSL/TLS connection"”的异常,那么请设置

hwrRequest.KeepAlive = true;

 

如果您使用的是CreateFromSignedFile来创建证书,那么请您务必注意,即使CreateFromSignedFile没有能够从文件中创建证书,甚至即使没有找到该文件,他也不会抛出异常,也不返回null,只是他的各个字段为null

所以,。。。,还是请使用CreateFromCertFile好了。

 

至于如何在个人证书存储区获取证书,参看下面的blog

参看:

WSE2.0X509安全令牌的使用

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节点下加:
   

2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
  表示引用的是 wse2.0
下加:
     
       
     
   
configuration 节点下加:

     
   
 
这个 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时,如果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

”了,它的定义参见http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemsecuritycryptographyx509certificatesx509certificateclasstopic.asp

之后我们就可以

/// 构建请求的HttpWebRequest对象

HttpWebRequest hwrRequest = (HttpWebRequest)WebRequest.Create(

                                   strValidatePageURL);

/// 从本地文件中加载证书

hwrRequest.ClientCertificates.Add(X509Certificate.CreateFromCertFile("c://motor.https.pem.cer"));

 

这是一个较简单的办法。

如果你遇到了“The underlying connection was closed. Could not establish a secure SSL/TLS connection"”的异常,那么请设置

hwrRequest.KeepAlive = true;

 

如果您使用的是CreateFromSignedFile来创建证书,那么请您务必注意,即使CreateFromSignedFile没有能够从文件中创建证书,甚至即使没有找到该文件,他也不会抛出异常,也不返回null,只是他的各个字段为null

所以,。。。,还是请使用CreateFromCertFile好了。

 

至于如何在个人证书存储区获取证书,参看下面的blog

参看:

WSE2.0X509安全令牌的使用

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节点下加:
   

2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
  表示引用的是 wse2.0
下加:
     
       
     
   
configuration 节点下加:

     
   
 
这个 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时,如果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





http://www.niftyadmin.cn/n/3649306.html

相关文章

JDBC的使用(一)

Java 中的数据存储技术 在Java中,数据库存取技术可分为如下几类: ①JDBC直接访问数据库 ②JDO技术 ③第三方O/R工具,如Hibernate, ibatis 等 JDBC是java访问数据库的基石,JDO, Hibernate等…

若依项目(前后端分离)

最近在基于若依项目二次开发中,遇到表设计问题,然后看了若依想项目前后端分离视频中,也是不太懂表的设计问题

Android常用Adapter代码例子

ArrayAdapter   总是感觉写自己的博客才更能够学到东西,网上尽管有很多好的资料,但是参差不齐,需要浪费大量时间才能够找到最需要的,索性写自己最需要的东西。 Adapter是适配器的意思,在Android中大量的使用到了List…

node中的数据持久化之mongoDB

一、什么是mongoDB MongoDB是一种开源的非关系型数据库,正如它的名字所表示的,MongoDB支持的数据结构非常松散,是一种以bson格式(一种json的存储形式)的文档存储方式为主,支持的数据结构类型更加丰富的NoS…

在JavaScript中将toLocaleString与数字,数组或日期一起使用

toLocaleString is a built-in JavaScript method used to convert the date and time to a string using the system locales. 🤓🚀 toLocaleString是内置JavaScript方法,用于使用系统区域设置将日期和时间转换为字符串。 🤓&…

Android开发过程中的几个小知识点

1. 在程序的Manifest里面对应的Activity里面添加android:windowSoftInputMode"adjustResize"属性,可以实现打开输入法时,界面自动上移,不被输入法遮盖。 2. 添加按钮的按下效果时,可以在drawable文件夹下新建一个xml文件…

[dotNET]如何利用ConfigurationSettings.AppSettings.GetValues读取配置文件中多个同Key的value

编写者:郑昀Ultrapower默认情况下,string[] strArray System.Configuration.ConfigurationSettings.AppSettings.GetValues("Uri");是无法读取配置文件中多个同Key的value的。如下所示的配置:用MSDN告诉我们的GetValues是读不到的…

node.js 模块_如何创建Node.js模块

node.js 模块The author selected the Open Internet/Free Speech Fund to receive a donation as part of the Write for DOnations program. 作者选择了“ 开放互联网/言论自由基金会”作为“ Write for DOnations”计划的一部分来接受捐赠。 介绍 (Introduction) In Node.j…