Accessing a web service and a HTTP interface using certificate authentication

后端 未结 1 693
渐次进展
渐次进展 2020-12-10 04:44

It is the first time I have to use certificate authentication. A commercial partner expose two services, a XML Web Service and a HTTP service. I have to access both of them

1条回答
  •  有刺的猬
    2020-12-10 05:40

    The following should help you identify the issue, here are two methods to test SSL connectivity one tests the site whilst the other is a callback method to identify why SSL failed. If nothing else it should give you a better idea why it is failing.

    When the method is called it will pop up with the select certificate dialog box, obviously when you do this for real you'll want to read from the cert store automatically. The reason I have put this in is because if no valid certificate is found then you will know your problem is with the way the certificate is installed.

    The best thing to do is put this code in a simple console app:

    using System.Security.Cryptography.X509Certificates;
    using System.Net.Security;
    using System.Net;
    
    private static void CheckSite(string url, string method)
    {
        X509Certificate2 cert = null;
        ServicePointManager.ServerCertificateValidationCallback += ValidateRemoteCertificate;
    
        X509Store store = new X509Store(StoreLocation.LocalMachine);
        store.Open(OpenFlags.ReadOnly | OpenFlags.OpenExistingOnly);
        X509Certificate2Collection certcollection = (X509Certificate2Collection)store.Certificates;
        // pick a certificate from the store
        cert = X509Certificate2UI.SelectFromCollection(certcollection, 
                "Caption",
                "Message", X509SelectionFlag.SingleSelection)[0];
    
        store.Close();
    
        HttpWebRequest ws = (HttpWebRequest)WebRequest.Create(url);
        ws.Credentials = CredentialCache.DefaultCredentials;
        ws.Method = method;
        if (cert != null)
            ws.ClientCertificates.Add(cert);
    
        using (HttpWebResponse webResponse = (HttpWebResponse)ws.GetResponse())
        {
            using (Stream responseStream = webResponse.GetResponseStream())
            {
                using (StreamReader responseStreamReader = new StreamReader(responseStream, true))
                {
                    string response = responseStreamReader.ReadToEnd();
                    Console.WriteLine(response);
                    responseStreamReader.Close();
                }
    
                responseStream.Close();
            }
            webResponse.Close();
        }
    }
    
    /// 
    /// Certificate validation callback.
    /// 
    private static bool ValidateRemoteCertificate(object sender, X509Certificate cert, X509Chain chain, SslPolicyErrors error)
    {
        // If the certificate is a valid, signed certificate, return true.
        if (error == System.Net.Security.SslPolicyErrors.None)
        {
            return true;
        }
    
        Console.WriteLine("X509Certificate [{0}] Policy Error: '{1}'",
            cert.Subject,
            error.ToString());
    
        return false;
    }
    

    0 讨论(0)
提交回复
热议问题