uwp - 如何在通用 Windows 应用程序中使用 System.Net.Http.HttpClient 忽略自签名证书

标签 uwp portable-class-library dotnet-httpclient self-signed

我正在创建一个可移植类库,这意味着据我所知,我必须使用 System.Net.Http.HttpClient 来调用我的 Web API。挑战在于,对于我的通用 Windows 应用程序,我无法弄清楚如何忽略由于 API 服务器可以拥有自签名证书而返回的错误。任何建议将不胜感激。

更新:我无法导入任何证书,因为这将是一个在各种组织的各种设备上运行的应用程序,让它们将自签名证书导入到运行该应用程序的每个设备上是不切实际的。

最佳答案

不是一个选项,因为 System.Net.ServicePointManager.CertificatePolicy在 UWP 中不可用。如果您使用 Windows.Web.Http.HttpClient相反,您可以 ignore self signed certificates .

更新:

再想一想,如果将自签名证书错误添加到应用程序的根证书中,则可以忽略它。

两种选择:

  • 使用 API 安装它:
    IBuffer buffer = await FileIO.ReadBufferAsync(file);
    Certificate rootCert = new Certificate(buffer); 
    CertificateStore rootStore = CertificateStores.TrustedRootCertificationAuthorities;
    rootStore.Add(rootCert);
    
  • 把它包括在你身上 Package.appxmanifest > 声明 > 证书 > 添加 并设置:
  • 店铺名称:root
  • 内容:证书文件路径

  • 使用这两种选择中的任何一种,您都将停止获得:

    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/

    相关文章:

    来自可移植类库 : Value cannot be null or an empty string. 的 Nuget 参数名称:profileValue

    c# - HttpClientFactory BadRequest?

    c# - 处理 UWP 应用程序中的 alt 键

    c# - 使用 xamarin.forms 跨平台 C# 在设备日历上设置提醒

    c# - 在特定位置显示 UI 元素上方的浮出控件

    xamarin - 如何在 Xamarin PCL 中显示错误消息

    c# - System.Net.Http.HttpClient 如何选择认证类型?

    c# - 如何在 .NET Core 中使用 HttpClientHandler 和 HttpClientFactory

    xaml - UWP页面过渡动画

    c# - VirtualKey 检测何时按下