title: C#, How can I add the certificate to the HttpClientHandler?
body: <p>I need to send data to a web api which needs a certificate to authorize. But I've no clue how I can add the certificate to the handler in .Net 4.5. This is the code:</p>
  
  <pre><code>// Import the certificate
  X509Certificate2Collection certificates = new X509Certificate2Collection();
  certificates.Import(pathToCertificate, password, X509KeyStorageFlags.MachineKeySet | X509KeyStorageFlags.PersistKeySet);
  ServicePointManager.ServerCertificateValidationCallback = (sender, certificate, chain, sslPolicyErrors) =&gt; true;
  
  using (var handler = new HttpClientHandler() { })
  {
      using (var client = new HttpClient(handler))
      {
          client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
  
  
          // Build the JSON
          string json = JsonConvert.SerializeObject(userData, Formatting.Indented);
          Console.WriteLine(json);
  
          var httpContent = new StringContent(json, Encoding.UTF8, "application/json");
  
          var processResult = await client.PostAsync(uriToServer, httpContent);
          var responseBody = processResult.Content.ReadAsStringAsync().Result;
  
          Console.WriteLine(responseBody);
          //return JsonConvert.DeserializeObject&lt;string&gt;(responseBody);
      }
  }
  </code></pre>
  
  <p>I searched google and found a "HttpClientHandler.ClientCertificates.Add" but that is .Net Core and not 4.5.</p>
  
  <p>Is this possible with HttpClient or do I have to use the HttpWebRequest class?</p>
  
  <p><strong>UPDATE:</strong></p>
  
  <p>Related on the given answer from "Peter Bons" (thanks again) and since the using is only try-finally I changed the code to this:</p>
  
  <pre><code>// Create an WebRequestHandler instance
  var handler = new WebRequestHandler();
  
  // Add the certificate
  var certFile = Path.Combine(pathToWorkingDirectory, "SERVER.PFX");
  handler.ClientCertificates.Add(new X509Certificate2(certFile, "password"));
  
  var client = new HttpClient(handler);
  try
  {
      //client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
  
      var httpContent = new StringContent(json, Encoding.UTF8, "application/json");
  
      //var processResult = await client.GetAsync("https://server/login");
      var processResult = await client.PostAsync("https://server/p/m", httpContent);
      var responseBody = processResult.Content.ReadAsStringAsync().Result;
  
      Console.WriteLine(responseBody);
  }
  catch (Exception e)
  {
      Console.WriteLine($"ERROR: {e}");
  }
  finally
  {
      client.Dispose();
  }
  </code></pre>
  
  <p>Unfortunately the Server response with 403 forbidden...
  How can I check if the authorization is working with the certificate?</p>
  
  <p><strong>UPDATE 2:</strong></p>
  
  <p>It seems like the answer to this question lies in the debugging:
  <a href="https://i.stack.imgur.com/xuA3D.png" rel="noreferrer"><img src="https://i.stack.imgur.com/xuA3D.png" alt="enter image description here"></a></p>
  
  <p>I tested the password and with the wrong password for the certificate a exception happens. So I am not sure if the server is accepting the certificate but the server path is dead or the server is not accepting the certificate.</p>
  
  <p><strong>UPDATE 3:</strong></p>
  
  <p>Looking at the response object showing more information but not if the certificate caused the error.
  <a href="https://i.stack.imgur.com/eluxU.png" rel="noreferrer"><img src="https://i.stack.imgur.com/eluxU.png" alt="enter image description here"></a></p>
  
  <p>But there is a Exception inside the content?
  <a href="https://i.stack.imgur.com/7tPfs.png" rel="noreferrer"><img src="https://i.stack.imgur.com/7tPfs.png" alt="enter image description here"></a></p>
  
  <p><strong>UPDATE 4:</strong></p>
  
  <p>Unfortunately, the server never received the certificate... </p>
  
  <p><strong>LAST UPDATE</strong></p>
  
  <p>The problem was the certificate I got from our sysadmin. Code works. Problem solved. :)</p>