zoukankan      html  css  js  c++  java
  • 调用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的电子杂志上有一期有专门的讲解,我就不多说了。

  • 相关阅读:
    关于android 代码生成布局中遇到的一些问题
    关于选择移动开发平台(android,ios,wp7)的一些看法
    如何成为一个C++高级程序员(转帖)
    最新Windows平台下Android源码的下载(转+原)
    峨眉之巅放歌
    孝感人间
    迁芸(帮客户名字作诗)
    载春
    杨美花(帮客户名字作诗)
    人生几度温泉夜
  • 原文地址:https://www.cnblogs.com/ocean/p/51217.html
Copyright © 2011-2022 走看看