locked
Private Key is missing- error RRS feed

  • Question

  • User-1034672953 posted

    hi

    iam writing a code to access a service on another server

    the certificate was installed with the private key on my server

    when i run the test 

      if (cer.HasPrivateKey)
                {
                    Response.Write("OK");
    
                }
                else
                {
                    Response.Write("Nooo");
                }

    i got ok = so the certificate has a private key

    but when i run the whole code

     try
            {
    
    
                //  ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
                HttpWebRequest httpWReq =
                     (HttpWebRequest)WebRequest.Create("https://gw.bisan.com/api/apdemo_6");
    
                Encoding encoding = new UTF8Encoding();
                string postData = @"{""user"":""cube"",""password"":""258970"",""command"":""table"",""table"":""currency"",""filters"":[{""field"":""code"",""operation"":""!\u003d"",""value"":""01""}],""fields"":[""symbol"",""rate""]}";
                byte[] data = encoding.GetBytes(postData);
    
                httpWReq.ProtocolVersion = HttpVersion.Version11;
                httpWReq.Method = "POST";
                httpWReq.ContentType = "application/json";//charset=UTF-8";
    
    
                var certificate = @"\www.cube-its.com.pfx";
                X509Certificate2 cer = new X509Certificate2(new X509Certificate2(Server.MapPath(certificate), "xxx"));
    
                httpWReq.ClientCertificates.Add(cer);
    
                Response.Write(cer.PrivateKey.ToString());
                if (cer.HasPrivateKey)
                {
                    Response.Write("OK");
    
                }
                else
                {
                    Response.Write("Nooo");
                }
    
                httpWReq.ContentLength = data.Length;
          
               
    
                Stream stream = httpWReq.GetRequestStream();
                stream.Write(data, 0, data.Length);
                stream.Close();
                
                HttpWebResponse response = (HttpWebResponse)httpWReq.GetResponse();
    
                string s = response.ToString();
                StreamReader reader = new StreamReader(response.GetResponseStream());
                String jsonresponse = "";
                String temp = null;
                while ((temp = reader.ReadLine()) != null)
                {
                    jsonresponse += temp;
                }
    
                Response.Write("Json Response : " + jsonresponse);
    
            }
            catch (WebException ex1)
            {
                { Response.Write("Exception 2 : " + ex1.Message); }
            }
            catch (Exception ex) { Response.Write("Exception : " + ex.Message); }

    i always got  server error 400 bad request

    when i cantact the company they say that the request is missing the private key

    so what shall i do 'thanx

    Tuesday, February 24, 2015 7:01 AM

Answers