ocean

嘿嘿,我的地盘由我来
随笔 - 96, 文章 - 0, 评论 - 1032, 引用 - 23
数据加载中……

调用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的电子杂志上有一期有专门的讲解,我就不多说了。

posted on 2004-10-12 13:45 ocean 阅读(2171) 评论(3)  编辑 收藏

评论

#1楼    回复  引用    

i also met the similar issue in pre project and found a way to resolve it. you can see :http://www.cnblogs.com/duibudui/archive/2004/07/09/29702.aspx
2004-10-12 16:13 | duibudui [未注册用户]

#2楼    回复  引用  查看    

http://www.cnblogs.com/elevenwolf/archive/2004/06/15/15932.aspx
2004-10-12 17:35 | 浪漫十一狼      

#3楼    回复  引用    

多谢!

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

首先加上引用“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好了。
2005-03-28 23:17 | zhengyun [未注册用户]

标题  
姓名  
主页
Email (博主才能看到) 
验证码 *  看不清,换一张 [登录][注册]
内容(请不要发表任何与政治相关的内容)  
  登录  使用高级评论  新用户注册  返回页首  恢复上次提交      
"五向定位"职业成长路线公开课(上海、南京、大连)
Google站内搜索


相关链接: