1

I want to consume a web service (https://realtime-listings-api.webservices.zpg.co.uk/sandbox/v1/listing/list) and as per there documentation (https://realtime-listings.webservices.zpg.co.uk/docs/latest/documentation.html) I have to send .crt and .pem file for authentication. I am able to load .crt file but for .pem I am getting error that Cannot find the requested object . I have tried different method to load PEM file.

I have followed following threads but still not able to load X509Certificate from .pem file.

My code is as below

var webAddr = "https://realtime-listings-api.webservices.zpg.co.uk/sandbox/v1/listing/list";
                    var httpWebRequest = (HttpWebRequest)WebRequest.Create(webAddr);
                    httpWebRequest.ContentType = "application/json; profile=http://realtime-listings.webservices.zpg.co.uk/docs/v1.1/schemas/listing/list.json";
                    httpWebRequest.Method = "POST";

httpWebRequest.ClientCertificates.Add(X509Certificate.CreateFromCertFile(@"E:\ProcessZooplaData\zpg_realtime_listings_14810206-20261204.crt"));
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;

Now till here everything is Okay now If I try to load .pem file then I am getting error

var pem = System.IO.File.ReadAllText(@"E:\\ProcessZooplaData\\private.pem");
byte[] certBuffer = GetBytesFromPEM(pem, "RSA PRIVATE KEY");
                    var certificate = new X509Certificate(certBuffer);
                    httpWebRequest.ClientCertificates.Add(certificate);



byte[] GetBytesFromPEM(string pemString, string section)
        {
            var header = String.Format("-----BEGIN {0}-----", section);
            var footer = String.Format("-----END {0}-----", section);

            var start = pemString.IndexOf(header, StringComparison.Ordinal);
            if (start < 0)
                return null;

            start += header.Length;
            var end = pemString.IndexOf(footer, start, StringComparison.Ordinal) - start;

            if (end < 0)
                return null;

            return Convert.FromBase64String(pemString.Substring(start, end));
        }

I am getting error here that Cannot find the requested object . Rest of code is as below

using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
                    {
                        string json = "{\"branch_reference\":\"test\"}";

                        streamWriter.Write(json);
                        streamWriter.Flush();
                    }

                    var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
                    using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
                    {
                        var result = streamReader.ReadToEnd();
                        //return result;
                    }

I have tried following threads for reference how to get private key from PEM file?

http://pages.infinit.net/ctech/20040812-0816.html

Community
  • 1
  • 1
Mahajan344
  • 2,492
  • 6
  • 39
  • 90

1 Answers1

0

If your certificate has already been loaded into your cert store, then you can do the following:

var requestHandler = new WebRequestHandler();
var store = new X509Store("My", StoreLocation.LocalMachine);
store.Open(OpenFlags.ReadOnly);
var certificates = store.Certificates.Find(X509FindType.FindBySubjectDistinguishedName, certificateName, true);
if (certificates.Count > 0)
    requestHandler.ClientCertificates.Add(certificates[0]);
else
    throw new Exception(string.Format("Can't find certificate {0}", certificateName));
using (var client = new HttpClient(requestHandler))
{
    do work!
}

which should add the cert from the store to the connection.

Then you'll need to open up the cert store in the Certificates (run mmc, add Certificates to the console for the local computer store), browse to your certificate, right click it and select All Tasks > Manage Private Keys and grant the user account your application will run under "read" access to the cert's private key and you won't need to open any files to do this.

Of course, if you're doing this on a shared hosting environment where you don't get access to the certs or can't install them into the cert store, that's a different problem.

Barry Colebank Jr
  • 1,939
  • 2
  • 16
  • 16