Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to ignor self signed certificates with System.Net.Http.HttpClient in Universal Windows App

I am creating a Portable Class Library which means I must use System.Net.Http.HttpClient to call my web APIs as far as I understand. The challenge is that for my Universal Windows App, I cannot figure out how to ignore the error that is returned due to the fact the fact the API server can have a self signed certificate. Any suggestions would be greatly appreciated.

UPDATE: I cannot import any certificates as this will be an application that runs on various devices in various organizations and is not practical to have them import a self signed certificate onto every device running the application.

like image 522
George M Ceaser Jr Avatar asked Jan 16 '16 01:01

George M Ceaser Jr


People also ask

How do I configure a custom httpclient for self signed certificates?

We configure a custom HttpClient. We begin by setting up an SSLContext using the SSLContextBuilder and use the TrustSelfSignedStrategy class to allow self signed certificates. Using the NoopHostnameVerifier essentially turns hostname verification off.

How to ignore self signed certificate errors?

If you use Windows.Web.Http.HttpClient instead, then you can ignore self signed certificates. In a second thought, you can ignore self signed certificate errors if you add it to the root certificates of the app.

How to ignore self signed certificate errors in UWP?

Not an option since System.Net.ServicePointManager.CertificatePolicy is not available in UWP. If you use Windows.Web.Http.HttpClient instead, then you can ignore self signed certificates. In a second thought, you can ignore self signed certificate errors if you add it to the root certificates of the app.

Can I use self-signed certificates with dotNET Dev-Certs?

In this guide, you'll cover using self-signed certificates with dotnet dev-certs, and other options like PowerShell and OpenSSL. You can then validate that the certificate will load using an example such as an ASP.NET Core app hosted in a container.


1 Answers

Not an option since System.Net.ServicePointManager.CertificatePolicy is not available in UWP. If you use Windows.Web.Http.HttpClient instead, then you can ignore self signed certificates.

UPDATE:

In a second thought, you can ignore self signed certificate errors if you add it to the root certificates of the app.

Two options:

  • Install it with APIs:

    IBuffer buffer = await FileIO.ReadBufferAsync(file);
    Certificate rootCert = new Certificate(buffer); 
    CertificateStore rootStore = CertificateStores.TrustedRootCertificationAuthorities;
    rootStore.Add(rootCert);
    
  • Include it in you Package.appxmanifest > Declarations > Certificates > Add and set:

    • Store name: root
    • Content: path to certificate file

With any of both options, you will stop getting:

System.Net.WebException: The underlying connection was closed: Could not establish trust relationship for the SSL/TLS secure channel. ---> System.Security.Authentication.AuthenticationException: The remote certificate is invalid according to the validation procedure.

like image 190
kiewic Avatar answered Oct 09 '22 02:10

kiewic