【问题标题】:C# Fetching server certificates from HTTPS URL throws System.PlatformNotSupportedException exceptionC# 从 HTTPS URL 获取服务器证书引发 System.PlatformNotSupportedException 异常
【发布时间】:2018-06-12 13:22:14
【问题描述】:

我正在使用以下代码 sn-p 以从 C# 中的 HTTPS URL 网站获取(或获取)证书。

这里发布了类似的代码 -> Open Certificate Information from Web Browser ControlC# Download all https certificates from a website 但没有什么能帮助我,我在下面的行中不断收到异常 #

           X509Certificate cert = request.ServicePoint.Certificate;

例外:

   System.PlatformNotSupportedException: 'Operation is not supported on this platform.'

代码:

 private static void Main(string[] args)
    {
        String sslServerHost = "https://docs.genesys.com/Documentation"
        HttpWebRequest request = (HttpWebRequest) WebRequest.Create( sslServerHost);
        request.AllowAutoRedirect = false;

        HttpWebResponse response = (HttpWebResponse)request.GetResponse();
        response.Close();


       X509Certificate cert = request.ServicePoint.Certificate;

        //convert the X509Certificate to an X509Certificate2 object by passing it into the constructor
       X509Certificate2 cert2 = new X509Certificate2(cert);

        X509Store userCaStore = new X509Store(storeName: StoreName.Root, storeLocation: StoreLocation.CurrentUser);
        try
        {
            userCaStore.Open(OpenFlags.ReadOnly);
            userCaStore.Add(cert2);

            Stream dataStream = response.GetResponseStream();
            StreamReader reader = new StreamReader(dataStream);
            string responseFromServer = reader.ReadToEnd();
            Debug.WriteLine(responseFromServer);
            Console.WriteLine(responseFromServer);

        } catch (Exception e)
        {
            Console.WriteLine(e.Message);
        }

    }

更新

.NET Core 项目不支持ServicePoint.Certificate,因此我尝试使用以下代码使用服务器证书验证回调:

request.ServerCertificateValidationCallback += (sender, validatedCert, chain, 
errors) => { X509Certificate cert = new X509Certificate2(validatedCert); 
Console.WriteLine("Inside callback" + cert); return errors == 
System.Net.Security.SslPolicyErrors.None; }

上面的回调似乎没有被调用。谁能告诉我上面的回调什么时候执行。

此外,在尝试连接到 HTTPS URL 而不将服务器证书添加到信任存储区时,我没有遇到任何异常。鉴于我没有忽略证书验证,任何人都可以告诉我为什么在尝试访问 HTTPS URL 时我们没有收到任何异常(在 .NET 应用程序中)。

【问题讨论】:

  • @mjwills,感谢您的指出。我已经用异常的详细信息更新了问题!
  • 谷歌搜索request.ServicePoint.Certificate PlatformNotSupportedException 是否有帮助(例如github.com/dotnet/corefx/issues/25816)?
  • @mjwills:是的,我曾尝试在回调 ServerCertificateValidationCallback 中打印控制台日志。证书详细信息未显示在控制台中,这意味着未触发回调。
  • 你的代码还在调用request.ServicePoint.Certificate;吗?您是在 .NET Core 还是 .NET Framework 上运行?
  • @mjwills :我在 .NET Core 上运行。我已经删除了request.ServicePoint.Certificate; 行并用回调代替了它.................. ......................request.ServerCertificateValidationCallback += (sender, validatedCert, chain, errors) => { X509Certificate cert = new X509Certificate2(validatedCert); Console.WriteLine("Inside callback"+cert); return errors == System.Net.Security.SslPolicyErrors.None; };

标签: c# .net ssl ssl-certificate x509certificate


【解决方案1】:

在 .Net 核心上使用旧的 HttpWebRequest 是在找麻烦。 您应该切换到 HttpClient 并在 HttpClientHandler 实例上设置 callback

  var handler = new HttpClientHandler();
  handler.ServerCertificateCustomValidationCallback = (request,cert,chain,error) => true;
  var client = new HttpClient(handler);

【讨论】:

    猜你喜欢
    • 2018-11-24
    • 1970-01-01
    • 2020-08-22
    • 2014-08-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-03
    • 1970-01-01
    相关资源
    最近更新 更多