Using client certificate not in certificate store

前端 未结 4 1883
小鲜肉
小鲜肉 2021-01-30 23:45

I\'m trying to authenticate myself against WebService using my client certificate, but, for some reasons (I explain), I don\'t want to load certificate from store, rather read i

4条回答
  •  独厮守ぢ
    2021-01-31 00:29

    What type of file is PathToCertificate? If it's just a .cer file, it will not contain the private key for the certificate and trying to use that certificate for SSL/TLS will fail.

    However, if you have a PKCS7 or PKCS12 file that includes the public and private key for the certificate, your code will work (you might need to use the overload that takes a password if the private key has one).

    To test this, I went to http://www.mono-project.com/UsingClientCertificatesWithXSP and created my client.p12 file following those instructions. I also created a simple HTTPS server using HttpListener for testing.

    Then I compiled the following program into 'client.exe' and run like:

     client.exe https:/// client.p12 password
    

    where client.p12 is the PKCS12 file generated before and 'password' is the password I set for the private key of the certificate.

    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 ());
        }
    }
    

    Let me know if you want me to upload the server code and the certificates used on both sides of the test.

提交回复
热议问题