title: Programmly using SSL certificates in Windows 8 app
body: <p>I need help with this:</p>
  
  <p>Our backend is secured by self-signed certificate. Lets call it: OurMegaCoolCertificate.cer</p>
  
  <p>So, we have imported this certificate to our developers machines by using <strong>certmgr.msc</strong>. And now we can retrieve data from our backend using this code:</p>
  
  <pre><code>async public static Task&lt;string&gt; getData(string Id, string Type)
      {
          String url = "https://BACKEND/API/?Id=" + Id + "&amp;Type=" + Type;
          HttpClientHandler aHandler = new HttpClientHandler();
          aHandler.ClientCertificateOptions = ClientCertificateOption.Automatic;
          HttpClient aClient = new HttpClient(aHandler);
          aClient.DefaultRequestHeaders.ExpectContinue = false;
  
          aClient.DefaultRequestHeaders.MaxForwards = 3;
          Uri requestUri = new Uri(url);
          HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, requestUri);
          //request.Headers.ExpectContinue = false;
          var result = await aClient.GetAsync(requestUri, HttpCompletionOption.ResponseContentRead);
  
          var responseHeader = result.Headers;
          //Debug.WriteLine(responseHeader.WwwAuthenticate);
  
          var responseBody = await result.Content.ReadAsStringAsync();
  
          return responseBody;
      }
  </code></pre>
  
  <p>But offcourse we can't tell users of our application, to install the certificate manually, <strong>is there a way to add this certificate to project and use it? Or import to users machine programmly? Please guide me, i'm new to SSL security</strong></p>
  
  <p>I have managed to do this, no errors, but request fails, looks like request doesn't find certificate:</p>
  
  <pre><code>private async void GetOverHere()
      {
          //await Windows.Security.Cryptography.Certificates.CertificateEnrollmentManager.InstallCertificateAsync("",InstallOptions.None);
          StorageFolder packageLocation = Windows.ApplicationModel.Package.Current.InstalledLocation;
          StorageFolder certificateFolder = await packageLocation.GetFolderAsync("Certificates");
          StorageFile certificate = await certificateFolder.GetFileAsync("OurMegaCoolCertificate.cer");
  
          IBuffer buffer = await Windows.Storage.FileIO.ReadBufferAsync(certificate);
          string encodedString = Windows.Security.Cryptography.CryptographicBuffer.EncodeToBase64String(buffer);
  
          await Windows.Security.Cryptography.Certificates.CertificateEnrollmentManager.InstallCertificateAsync(encodedString, InstallOptions.None);
  
  
      }
  </code></pre>
  
  <p>Also we have tried to do this in manifest:</p>
  
  <pre><code>&lt;/Capabilities&gt;
  &lt;Extensions&gt;
  &lt;!--Certificates Extension--&gt;
   &lt;Extension Category="windows.certificates"&gt;
    &lt;Certificates&gt;
      &lt;Certificate StoreName="Root" Content="Assets\OurMegaCoolCertificate.cer" /&gt;
  
    &lt;/Certificates&gt;
  &lt;/Extension&gt;
  </code></pre>
  
  <p></p>
  
  <p>And again, when we import using certmgr.msc to Trusted Root Certificates - all works</p>