title: Windows Store App - C# - Https Client Authentication
body: <p>I'm trying to implement Https Client Authentication in my application but I am having trouble finding any documentation on how to do it.</p>
  
  <p>Looking through the MSDN documents I came up with this</p>
  
  <pre><code>// Certificate file in DER format (.cer or .p7b)   
  string CountriesFile = @"Assets\https-client.keystore.cer";
  StorageFolder InstallationFolder = Windows.ApplicationModel.Package.Current.InstalledLocation;
  StorageFile file = await InstallationFolder.GetFileAsync(CountriesFile);
  
  // Read the file into a buffer
  IBuffer buffer = await Windows.Storage.FileIO.ReadBufferAsync(file);
  
  // Create the Certificate object
  Certificate ClientCert = new Certificate(buffer);
  HttpBaseProtocolFilter aHBPF = new HttpBaseProtocolFilter();
  aHBPF.ClientCertificate = ClientCert;
  
  // Create our http client and send the request.
  HttpClient httpClient = new HttpClient(aHBPF);
  HttpResponseMessage response = await httpClient.SendRequestAsync(httpRequest, HttpCompletionOption.ResponseHeadersRead).AsTask(cts.Token);
  </code></pre>
  
  <p>I put this code together looking at the docs for <a href="http://msdn.microsoft.com/en-US/library/windows/apps/windows.web.http.httpclient.aspx" rel="nofollow">HttpClient</a>, <a href="http://msdn.microsoft.com/en-us/library/windows/apps/windows.web.http.filters.httpbaseprotocolfilter.aspx" rel="nofollow">HttpBaseProtocolFilter</a> and <a href="http://msdn.microsoft.com/en-us/library/windows/apps/windows.security.cryptography.certificates.certificate.aspx" rel="nofollow">Certificate</a>. Making the assumption that I should have the certificate in the required format and read the file into the <code>Certificate</code> class.</p>
  
  <p>The above code doesn't work and throws this error</p>
  
  <pre><code>An exception of type 'System.ArgumentException' occurred in MyLib.DLL but was not handled in user code
  WinRT information: The certificate specified is missing the required private key information.
  </code></pre>
  
  <p>I have tested my server set-up and it works with client auth through a browser, which leads me to two possible conclusions.</p>
  
  <ol>
  <li>The certificate file is in the wrong format (though I would have hoped the exception would get thrown when the <code>Certificate</code> class is constructed).</li>
  <li>This is not intended way to do it!</li>
  </ol>
  
  <p>Any one know how it should be done?</p>