Question

I am attempting to access an api which they had me install a local cert via mmc I have done so on my local windows 7 machine so I could test out the service. I'm using a simple winforms app to test. I'm assuming this is a self signed cert server side so I'm hoping to bypass the exception for dev purposes.

 HttpWebRequest req = (HttpWebRequest)WebRequest.Create(postData);
            req.Method = "POST";
            req.ContentType = "application/x-www-form-uriencoded";
            req.KeepAlive = true;
            req.AllowAutoRedirect = false;

            X509Store store = new X509Store(StoreName.Root,StoreLocation.LocalMachine);
            store.Open(OpenFlags.ReadOnly);
            X509Certificate cert = new X509Certificate();
            for (int i = 0; i < store.Certificates.Count; i++)
            {
                if (store.Certificates[i].SerialNumber == "XXXX")
                {
                    cert = store.Certificates[i];
                }
            }
            req.ClientCertificates.Add(cert);
            req.PreAuthenticate = true;

            try
            {
                using (WebResponse resp = req.GetResponse())
                {
                    Stream responseStream = resp.GetResponseStream();
                    using (StreamReader responseStreamReader = new
                    StreamReader(responseStream, new ASCIIEncoding()))
                    {
                        respString = responseStreamReader.ReadToEnd();
                    }
                }
            }
            catch (Exception ex)
            {
                // there is a problem
            }

I'm getting the following exception in the try catch: The underlying connection was closed: Could not establish trust relationship for the SSL/TLS secure channel.

  • I do have a certificate object in the variable "cert"
  • When I go to the api url in my browser I get the caution warning but then can proceed and get data.
  • I have tried the other fixes recommended on stackoverflow such as suppressing the exception.

    ServicePointManager.ServerCertificateValidationCallback = new
    RemoteCertificateValidationCallback ( delegate { return true; } );

I realize there are quite a few of these questions similar to this and trust me I have read through them all. Thanks for any suggestions they are appreciated.

Was it helpful?

Solution

Hey the following finally fixed it for me. I went to the api server address in my browser and exported their certificate to a file on my desktop. I then opened mmc and installed the certificate like in the following link:

http://support.esri.com/en/knowledgebase/techarticles/detail/38408

After I installed I had no issues calling the webservice and the exception went away.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top