почему сертификаты являются нулевыми при использовании .NET Core 2, но он прекрасно работает с .NET Framework 4.6.2? - PullRequest
0 голосов
/ 27 марта 2019

Я проводил некоторые тестовые миграции приложений .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 ... doты знаешь почему?

1 Ответ

1 голос
/ 18 апреля 2019

Смотрите обсуждение этого здесь: https://github.com/dotnet/corefx/issues/36979

Классы ServicePointManager и ServicePoint недоступны в .NET Core. Но вы можете сделать то же самое с HttpClient. HttpClient - более современный и предпочтительный HTTP API в .NET Framework и .NET Core.

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;
        }
    }
}
...