2

I am writing a program to submit xml files to a website using HTTPWebRequest. However, the following gives me an exception of "Cannot find the original signer": X509Certificate certificate = X509Certificate.CreateFromCertFile("myCert.p7b"); or X509Certificate2 certificate = new X509Certificate2("myCert.p7b");

Loading X509Certificate results in exception CryptographicException "Cannot find the original signer"

This link shows a similar (maybe identical) question, but the answer is kind of too general for me. I am wondering if anyone could provide a step by step instruction for the solution.

Thanks.

        X509Certificate certificate = X509Certificate.CreateFromCertFile("myCert.p7b");
        HttpWebRequest request = (HttpWebRequest)WebRequest.Create("https://api.foo.com/xmlService/versionX");
        request.ClientCertificates.Add(certificate);
        byte[] bytes;
        bytes = System.Text.Encoding.ASCII.GetBytes("myFile.xml");
        request.ContentType = "text/xml; encoding='utf-8'";
        request.ContentLength = bytes.Length;
        request.Method = "POST";
        Stream requestStream = request.GetRequestStream();
        requestStream.Write(bytes, 0, bytes.Length);
        requestStream.Close();
        HttpWebResponse response = (HttpWebResponse)request.GetResponse();
        if (response.StatusCode == HttpStatusCode.OK)
        {
            Stream responseStream = response.GetResponseStream();
            string responseStr = new StreamReader(responseStream).ReadToEnd();
            return responseStr;
        }
        return null;
Zhe Xu
  • 21
  • 2
  • 1
    Have you checked that the certificate pointed at by `CertificateFileName` actually does contain a private key? And if it does is the private key password protected? Import the certificate to windows, if there is a password it will ask for it, if it did not ask for a password open the imported certificate and see if it says "This certificate contains a private key" – Scott Chamberlain Aug 07 '19 at 20:12
  • Thanks for your help, Scott. I do have a private key( a .pfx file) and a password for this certificate (.p7b). I double clicked the .pfx and "imported successfully". – Zhe Xu Aug 07 '19 at 20:45
  • I am still working on the rest of the code, but this line removes the certificate exception. Hope this is the right solution: X509Certificate2 certificate = new X509Certificate2("cert.pfx”, File.ReadAllText(“password.txt”); – Zhe Xu Aug 07 '19 at 23:39

0 Answers0