not*_*row 16 c# web-services certificate
我正在尝试使用我的客户端证书对WebService进行身份验证,但是,由于某些原因(我解释),我不想从商店加载证书,而是从光盘读取它.
下列:
// gw is teh WebService client
X509Certificate cert = new X509Certificate(PathToCertificate);
_gw.ClientCertificates.Add(ClientCertificate());
ServicePointManager.ServerCertificateValidationCallback = (a,b,c,d) => true;
_gw.DoSomeCall();
总是返回403 - 服务不授权我.但是,当我将该证书保存到CertStore时,它可以工作.(如MSDN中所述.)
是否可以使用不在商店的证书?
(原因是,我得到了Windows服务(客户端)有时调用webservice(服务器),并且在未指定的时间后服务'忘记'我的证书并且没有对服务器进行授权,没有明显的原因)
Gon*_*alo 25
什么类型的文件是PathToCertificate?如果它只是一个.cer文件,它将不包含证书的私钥,并且尝试将该证书用于SSL/TLS将失败.
但是,如果您的PKCS7或PKCS12文件包含证书的公钥和私钥,则您的代码将起作用(如果私钥有密码,您可能需要使用带密码的重载).
为了测试这一点,我访问了http://www.mono-project.com/UsingClientCertificatesWithXSP并按照这些说明创建了我的client.p12文件.我还使用HttpListener创建了一个简单的HTTPS服务器进行测试.
然后我将以下程序编译成'client.exe'并运行如下:
 client.exe https://<MYSSLSERVER>/ client.p12 password
其中client.p12是之前生成的PKCS12文件,'password'是我为证书的私钥设置的密码.
using System;
using System.IO;
using System.Net;
using System.Security.Cryptography.X509Certificates;
using System.Text;
public class HttpWebRequestClientCertificateTest : ICertificatePolicy {
    public bool CheckValidationResult (ServicePoint sp, X509Certificate certificate,
            WebRequest request, int error)
    {
            return true; // server certificate's CA is not known to windows.
    }
    static void Main (string[] args)
    {
            string host = "https://localhost:1234/";
            if (args.Length > 0)
                    host = args[0];
            X509Certificate2 certificate = null;
            if (args.Length > 1) {
                    string password = null;
                    if (args.Length > 2)
                            password = args [2];
                    certificate = new X509Certificate2 (args[1], password);
            }
            ServicePointManager.CertificatePolicy = new HttpWebRequestClientCertificateTest ();
            HttpWebRequest req = (HttpWebRequest) WebRequest.Create (host);
            if (certificate != null)
                    req.ClientCertificates.Add (certificate);
            WebResponse resp = req.GetResponse ();
            Stream stream = resp.GetResponseStream ();
            StreamReader sr = new StreamReader (stream, Encoding.UTF8);
            Console.WriteLine (sr.ReadToEnd ());
    }
}
如果您希望我上传服务器代码和测试两侧使用的证书,请告诉我.
| 归档时间: | 
 | 
| 查看次数: | 29475 次 | 
| 最近记录: |