UWP ServicePointManager.ServerCertificateValidationCallback

Jas*_*son 9 c# xamarin xamarin.forms uwp .net-standard

我有一个 Xamarin.Forms 应用程序,它使用 ServicePointManager.ServerCertificateValidationCallback 类和方法实现证书固定。在 Android 和 iOS 上,这没有问题,因为它允许连接到证书密钥已固定的预期服务,并禁止连接那些我没有的服务。

但是,在 UWP 上,无论是否已固定证书密钥,都允许所有连接。我已经从证书验证方法中明确返回 false 并且仍然允许连接。我确定正在执行检查,因为我已经调试并逐步完成了证书验证方法。

即使我从验证检查中返回 false,也可能导致连接继续进行的原因是什么?

ServicePointManager.ServerCertificateValidationCallback = ValidateServerCertficate;
private static bool ValidateServerCertficate(
   object sender,
   X509Certificate certificate,
   X509Chain chain,
   SslPolicyErrors sslPolicyErrors
)
{
   return false;
}
Run Code Online (Sandbox Code Playgroud)

Jas*_*son 1

我能够通过以下方式解决这个问题:

  1. 在 UWP 项目中,双击 Package.appxmanifest 文件
  2. 在声明菜单下,从可用声明下拉列表中选择证书
  3. 单击添加按钮
  4. 选择独家信任选项

这会将以下 xml 放入 Package.appxmanifest 文件中:

<Extensions>
   <Extension Category="windows.certificates">
     <Certificates>
       <TrustFlags ExclusiveTrust="true" />
     </Certificates>
   </Extension>
</Extensions>
Run Code Online (Sandbox Code Playgroud)