Runtime: HttpWebRequest.ServicePoint.Certificates is null on .NET Core

Created on 9 Apr 2019  路  11Comments  路  Source: dotnet/runtime

I have been doing some test migrating .NET Framework 4.6.2 apps to .NET Core 2. I noticed that this particular app, a monitoring http is not working fine with Net Core 2. Can you please help me to verify what is happening?

c# static void Main(string[] args) { try { HttpWebRequest myhttpWebReqest = (HttpWebRequest)WebRequest.Create("https://www.google.com.mx/"); HttpWebResponse myHttpWebResponse = (HttpWebResponse)myhttpWebReqest.GetResponse(); Console.WriteLine(myHttpWebResponse.StatusCode); Console.WriteLine((int)myHttpWebResponse.StatusCode); Console.WriteLine(myhttpWebReqest.ServicePoint.Certificate.GetEffectiveDateString()); Console.WriteLine(myhttpWebReqest.ServicePoint.Certificate.GetExpirationDateString()); Console.WriteLine(myhttpWebReqest.ServicePoint.Certificate.Issuer); Console.WriteLine(myhttpWebReqest.ServicePoint.Certificate.Subject); } catch (Exception ex) { Console.WriteLine(ex.Message); if(ex.InnerException !=null) { Console.WriteLine(ex.InnerException); } } }

in the .NET Framework 4.6.2 i see the certificate data, in the .NET Core 2 i see myhttpWebReqest.ServicePoint.Certificate null ... do you know why?

you can see the same question in stack overflow here

area-System.Net.Http bug tenet-compatibility

Most helpful comment

Here is a simple example of how to do this with .NET Core and using HttpClient:

```c#
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;
    }
}

}
```

All 11 comments

@nguerrera @dougbu @anurse

@leecow This doesn't look ASP.NET related to me, it's referencing WebRequest. It looks like a corefx issue.

@karelz

Who may we contact?

HttpWebRequest is API which is obsolete - see https://github.com/dotnet/platform-compat/blob/master/docs/DE0003.md.
We ported only the most important parts of it to .NET Core.
The recommended Networking API is HttpClient.

Can you migrate to HttpClient?
If not, why do you need this API and how do you plan to use it?

Also, I was able to reproduce the problem on .NET Core 2.1 and verified it works differently on .NET Framework 4.7.2.

ServicePointManager and ServicePoint classes are no-op on .NET Core.

If you need to capture the TLS/SSL certificate property from doing a request, you can use HttpClient, HttpClientHandler and HttpClientHandler.ServerCertificateCustomValidationCallback property to get the certificate used for that request.

Here is a simple example of how to do this with .NET Core and using HttpClient:

```c#
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;
    }
}

}
```

This is basically the same as dotnet/runtime#24773

Duplicate of dotnet/runtime#24773

Was this page helpful?
0 / 5 - 0 ratings

Related issues

aggieben picture aggieben  路  3Comments

GitAntoinee picture GitAntoinee  路  3Comments

btecu picture btecu  路  3Comments

Timovzl picture Timovzl  路  3Comments

noahfalk picture noahfalk  路  3Comments