使用 .p12

问题描述

我有一个需要点击和验证的端点。

我有一个生成的 .p12 文件一个可用于验证的证书链(根证书和中间证书)。

这是我目前使用的代码,它给了我这个错误

System.Security.Authentication.AuthenticationException: The remote certificate is invalid according to the validation procedure.

var handler = new httpclienthandler();

var p12 = new X509Certificate2(
  Path.Combine(Environment.CurrentDirectory,"Assets/Certs/p12filename.p12"),"TheRealPassword");
            
handler.ClientCertificateOptions = ClientCertificateOption.Manual;
handler.ClientCertificates.Add(p12);
handler.ServerCertificateCustomValidationCallback = (httpRequestMessage,cert,chain,sslPolicyErrors) =>
{
    var root = new X509Certificate2(
        Path.Combine(Environment.CurrentDirectory,"Assets/Certs/Root.cer"));
                
    X509Chain CertificateChain = chain;
    CertificateChain.ChainPolicy.RevocationMode = X509RevocationMode.NoCheck;
                
    return CertificateChain.Build( root ); 
};

var client = new HttpClient(handler);

var url = "https://urlHere"
            
var request = new HttpRequestMessage()
  {
    RequestUri = new Uri(url),Method = HttpMethod.Get,};
            
var response = await client.SendAsync(request);

我不太精通使用证书,所以它可能是非常基本的东西。请注意,如果在 ServerCertificateCustomValidationCallback 中我返回 true 我没有问题。

更新: 澄清一下,我如何使用我的 .p12 和相应的根证书和中间证书来验证我遇到的终点。我的假设基于在回调中返回 true 给我我想要的东西是我需要在这里做这些检查。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)