我正在创建一个可移植类库,这意味着据我所知,我必须使用 System.Net.Http.HttpClient 来调用我的 Web API。挑战在于,对于我的通用 Windows 应用程序,我无法弄清楚如何忽略由于 API 服务器可以拥有自签名证书而返回的错误。任何建议将不胜感激。
更新:我无法导入任何证书,因为这将是一个在各种组织的各种设备上运行的应用程序,让它们将自签名证书导入到运行该应用程序的每个设备上是不切实际的。
最佳答案
不是一个选项,因为 System.Net.ServicePointManager.CertificatePolicy
在 UWP 中不可用。如果您使用 Windows.Web.Http.HttpClient
相反,您可以 ignore self signed certificates .
更新:
再想一想,如果将自签名证书错误添加到应用程序的根证书中,则可以忽略它。
两种选择:
IBuffer buffer = await FileIO.ReadBufferAsync(file);
Certificate rootCert = new Certificate(buffer);
CertificateStore rootStore = CertificateStores.TrustedRootCertificationAuthorities;
rootStore.Add(rootCert);
使用这两种选择中的任何一种,您都将停止获得:
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.
关于uwp - 如何在通用 Windows 应用程序中使用 System.Net.Http.HttpClient 忽略自签名证书,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34822640/