title: POST request using specific SSL certificate only works until log off or restart of PC
body: <ol>
  <li><p><strong>My task goes like this:</strong>
  I need to perform POST requests over HTTPS using a certain SSL certificate to submit some information to an external API.</p>
  </li>
  <li><p><strong>What I did:</strong>
  I installed the SSL certificate in the Local Machine certificate's store using the password I was given for that specific certificate. Then, I select the certificate programatically inside my makePost function (I identify the certificate by its product key) and use it to perform the POST request.</p>
  </li>
  <li><p><strong>How it works:</strong>
  Using the actions mentioned above I am able to send information to the external API using that SSL certificate and it all works like a charm, until I log off or let the computer into sleep mode or restart the computer. (I am now talking about the IDE environment in which I am developing and testing my solution: VisualStudio).</p>
  </li>
  <li><p><strong>What I need:</strong>
  I need to understand why after the restart the post request breakes with tje folowing error: &quot;The request was aborted: Could not create SSL/TLS secure channel.&quot;. I know this is a very general error but this is all that the exception gives me. I checked beforehand if, after restart or log off the certificate is still there, in the store and it is. I even wrote to my log to see if it is identified corectly before and after the log off or restart. The certificate found before and after these operations are identical.</p>
  </li>
  <li><p><strong>Mentions about my architecture:</strong></p>
  </li>
  </ol>
  <ul>
  <li>The Prototype class is a personalised class that holds all the
  information I need to be sent. I am using MVC so these functions
  exist in one of my Model classes.</li>
  <li>I am using .NET Framework 4.7.2</li>
  </ul>
  <p>I will list here the function that searches for the SSL certificate:</p>
  <pre><code>private static X509Certificate2 GetCertificateFromStore(string serialNumber)
      {
          X509Store store = new X509Store(StoreLocation.LocalMachine);
          try
          {
              store.Open(OpenFlags.ReadOnly);
  
              X509Certificate2 signingCert = null;
  
              foreach(X509Certificate2 item in store.Certificates)
              {
                  if (item.SerialNumber == serialNumber)
                  { 
                      signingCert = item;
                      break;
                  }
              }
  
              if (signingCert != null)
              { 
                  WriteErrorLog(&quot;The SSL certificate used is:&quot; + Environment.NewLine + &quot;\t Issuer=&quot; + signingCert.Issuer + &quot;| SerialNumber=&quot; + signingCert.SerialNumber); 
              }
              else
              {
                  WriteErrorLog(&quot;The SSL certificate with the SerialNumber &quot; + serialNumber + &quot; is not isntalled in your computer or it couldn't be found.&quot;);
              }
              
              return signingCert;
          }
          finally
          {
              store.Close();
          }
      }
  </code></pre>
  <p>And here the function that makes the postRequest:</p>
  <pre><code>private string makePostRequest(Prototype prototype)
      {
          string result = &quot;&quot;;
          try
          {
              if (certificat != null)
              {
                  HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(API_URL);
                  httpWebRequest.ContentType = &quot;application/json&quot;;
                  httpWebRequest.Headers[&quot;Authorization&quot;] = AUTH_KEY;
                  httpWebRequest.Method = &quot;POST&quot;;
                  httpWebRequest.ClientCertificates.Add(certificat);
                  using (StreamWriter streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
                  {
                      String json = Newtonsoft.Json.JsonConvert.SerializeObject(prototype);
                      streamWriter.Write(json);
                  }
                  HttpWebResponse httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
                  WriteErrorLog(&quot;\t httpResponse.StatusCode=&quot; + httpResponse.StatusCode.ToString());
                  result += httpResponse.StatusCode.ToString().ToUpper() + &quot; &quot;;
                  using (StreamReader streamReader = new StreamReader(httpResponse.GetResponseStream()))
                  {
                      result += streamReader.ReadToEnd();
                      return result;
                  }
              }
              else 
              {
                  WriteErrorLog(&quot;The SSL certificate for this request doesn't exist.&quot;);
              }
          }
          catch (Exception e)
          {
              result += e.Message + ((e.InnerException != null) ?&quot;; InnerException: &quot; + e.InnerException.Message : &quot;&quot; );
              WriteErrorLog(&quot;Error in makePostRequest: &quot; + e.Message + ((e.InnerException != null) ? 
                  &quot;;\n InnerException: &quot; + e.InnerException.Message : &quot;&quot;));
          }
          return result;
      }
  </code></pre>
  <p><em><strong>AUTH_KEY</strong></em>, <em><strong>API_URL</strong></em> and <em><strong>certificat</strong></em> are static attributes in my Controller (they need to stay static as they are the same for every user). The <em><strong>certificat</strong></em> variable's value is assigned to it from a function call like this:</p>
  <pre><code>public static X509Certificate2 certificat = GetCertificateFromStore(&quot;XXXX&quot;);
  </code></pre>