为什么使用 .NET Core 2 的证书为空,但它在 .NET Framework 4.6.2 中工作得很好?

why certificates are null using .NET Core 2 but it works just fine with .NET Framework 4.6.2?

我一直在做一些测试,将 .NET Framework 4.6.2 应用程序迁移到 .NET Core 2。我注意到这个特定的应用程序,一个监控 http 在 Net Core 2 上不能正常工作。你能帮我吗验证发生了什么?

static void Main(string[] args)
        {
            try
            {
                HttpWebRequest myhttpWebReqest = (HttpWebRequest)WebRequest.Create("https://www.google.com.mx/");
            System.Diagnostics.Stopwatch timer = new System.Diagnostics.Stopwatch();
            timer.Start();
            HttpWebResponse myHttpWebResponse = (HttpWebResponse)myhttpWebReqest.GetResponse();
            timer.Stop();
            TimeSpan timeSpan = timer.Elapsed;
            Console.WriteLine(timeSpan.ToString());
            Console.WriteLine();
            Console.WriteLine(myHttpWebResponse.StatusCode);
            Console.WriteLine((int)myHttpWebResponse.StatusCode);
            Console.WriteLine();
            Console.WriteLine(myhttpWebReqest.ServicePoint.Certificate.GetEffectiveDateString());
            Console.WriteLine();
            Console.WriteLine(myhttpWebReqest.ServicePoint.Certificate.GetExpirationDateString());
            Console.WriteLine();
            Console.WriteLine(myhttpWebReqest.ServicePoint.Certificate.Issuer);
            Console.WriteLine();
            Console.WriteLine(myhttpWebReqest.ServicePoint.Certificate.Subject);                
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
            if(ex.InnerException !=null)
            {
                Console.WriteLine(ex.InnerException);
            }
        }
        Console.ReadLine();
    }
}

在 .NET Framework 4.6.2 中我看到证书数据,在 .NET Core 2 中我看到 myhttpWebReqest.ServicePoint.Certificate null ...你知道为什么吗?

在此处查看对此的讨论:https://github.com/dotnet/corefx/issues/36979

ServicePointManager 和 ServicePoint 类 在 .NET Core 上是空操作。但是您可以使用 HttpClient 做类似的事情。 HttpClient 是 .NET Framework 和 .NET Core 中更现代和首选的 HTTP API。

using System;
using System.Net.Http;
using System.Net.Security;
using System.Security.Cryptography.X509Certificates;

namespace NetCoreConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            var handler = new HttpClientHandler();
            handler.ServerCertificateCustomValidationCallback = CustomCallback;
            var client = new HttpClient(handler);

            HttpResponseMessage response = client.GetAsync("https://www.google.com.mx/").GetAwaiter().GetResult();
            Console.WriteLine(response.StatusCode);
            Console.WriteLine((int)response.StatusCode);
        }

        private static bool CustomCallback(HttpRequestMessage arg1, X509Certificate2 arg2, X509Chain arg3, SslPolicyErrors arg4)
        {
            Console.WriteLine(arg2.GetEffectiveDateString());
            Console.WriteLine(arg2.GetExpirationDateString());
            Console.WriteLine(arg2.Issuer);
            Console.WriteLine(arg2.Subject);

            return arg4 == SslPolicyErrors.None;
        }
    }
}