How to invoke a Web Service which requires a certificate in C#?

Viewed 17332

I need to communicate with a third party which has a .asmx web service. This web service is using https. I have the required certificate (.pfx).

When first trying to add this service using Add Service Reference in Visual Studio, I got an error. I got passed this error by importing the certificate into the Personal store. After I did that, I tried to add the Service Reference again and it works. So now I can create an instance of the web service. Nice.

But now I want to invoke the service. And when I do that I get this error:

302 Notification: Digital Certificate Missing

So how can I tell my service to use the right certificate?

3 Answers

I was also facing "The request was aborted: Could not create SSL/TLS secure channel" while requesting a web service. Fixed issue with below code, hope this will help someone and save time.

 HttpWebRequest web_request = (HttpWebRequest)WebRequest.Create("https://yourservices/test.asmx");
        web_request.ContentType = "text/xml;charset=\"utf-8\"";

        ServicePointManager.Expect100Continue = true;
        ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3;
        web_request.ProtocolVersion = HttpVersion.Version10;

        X509Certificate2Collection certificates = new X509Certificate2Collection();
        certificates.Import(certName, password, X509KeyStorageFlags.MachineKeySet | X509KeyStorageFlags.PersistKeySet);

        web_request.ClientCertificates = certificates;
        web_request.Accept = "text/xml";
        web_request.Method = "POST";

        using (Stream stm = web_request.GetRequestStream())
        {
            using (StreamWriter stmw = new StreamWriter(stm))
            {
                stmw.Write(soap);
            }
        }

        WebResponse web_response = null;
        StreamReader reader = null;

        try
        {
            web_response = web_request.GetResponse();
            Stream responseStream = web_response.GetResponseStream();
            XmlDocument xml = new XmlDocument();

            reader = new StreamReader(responseStream);
            xml.LoadXml(reader.ReadToEnd());
        }
        catch (Exception webExp) {
            string exMessage = webExp.Message;
        }
Related