title: How to make secure request to REST, having signed Certificate with Public key, and Private Key generated with Certificate Request
body: <p>I need to make https request to RESTAPI. Before, i made Certificate Request and private key, using OpenSSL. After, my Certificate Request had been signed by provider and i took certificate. Thus i have certificate and private key.
  The question: How to make secure request using this certificate, private key and .net features.
  Now i provide code, that i already wrote with help of my intuition(note: i used BouncyCustle to read pem files):</p>
  
  <pre><code>static void Main(string[] args)
  {
      try
      {
          X509Certificate certificate = getCertificate("c:/xxx/certificate.pem");
          AsymmetricKeyParameter privateKey = getPrivateKey("c:/xxx/key.pem");
  
          HttpWebRequest webRequest = prepareWebRequest("https://xxxx", 
                  "GET", certificate, privateKey);
  
          WebResponse webResponse = webRequest.GetResponse();
      }
      catch(Exception e)
      {
          Console.WriteLine("Exception: {0}", e.Message);
          Console.Write("Stack trace: {0}", e.StackTrace);
      }
  
      Console.ReadKey();
  }
  
  private static X509Certificate getCertificate(string pathToSertificate)
  {
      X509Certificate certificateBouncyCustle;
  
      using (var fileStream = new FileStream(pathToSertificate, FileMode.Open))
      using (var streamReader = new StreamReader(fileStream))
      {
          var pemReader = new PemReader(streamReader);
          certificateBouncyCustle = (X509Certificate)pemReader.ReadObject();
      }
  
      return certificateBouncyCustle;
  }
  
  private static AsymmetricKeyParameter getPrivateKey(string pathToPrivateKey)
  {
      AsymmetricKeyParameter privateKey;
  
      using (var fileStream = new FileStream(pathToPrivateKey, FileMode.Open))
      using (var streamReader = new StreamReader(fileStream))
      {
          var pemReader = new PemReader(streamReader);
          privateKey = ((AsymmetricCipherKeyPair)pemReader.ReadObject()).Private;
      }
  
      return privateKey;
  }
  
  private static HttpWebRequest prepareWebRequest(string url, 
          string method, 
          X509Certificate certificate, 
          AsymmetricKeyParameter privateKey)
  {
      HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(url);
  
      httpWebRequest.Method = method;
      httpWebRequest.ClientCertificates.Add(DotNetUtilities.ToX509Certificate(certificate));                   
      httpWebRequest.ContentType = "application/xml;charset=UTF8";                        
  
      return httpWebRequest;
  }
  </code></pre>