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

拟墨画扇 提交于 2020-01-02 04:09:26

问题


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.


回答1:


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.



来源:https://stackoverflow.com/questions/34822640/how-to-ignor-self-signed-certificates-with-system-net-http-httpclient-in-univers

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!