Skip to content

ServicePoint retrieved via FindServicePoint does not contain server certificate #24773

Closed
dotnet/dotnet-api-docs
#4731

Description

@bojanrajkovic

A typical way to obtain the server certificate after making a request via HttpClient is to use ServicePointManager.FindServicePoint with the URI and grab the Certificate property off of that.

This works fine in .NET 4.5, as evidenced by this fragment from a workbook:

image

However, in a .NET Core application, this doesn't work:

image

You can also see the attached project in certificate-test.zip. It contains a sample you can download and dotnet run.

dotnet --info output:

.NET Command Line Tools (2.1.4)

Product Information:
 Version:            2.1.4
 Commit SHA-1 hash:  5e8add2190

Runtime Environment:
 OS Name:     Windows
 OS Version:  10.0.16299
 OS Platform: Windows
 RID:         win10-x64
 Base Path:   C:\Program Files\dotnet\sdk\2.1.4\

Microsoft .NET Core Shared Framework Host

  Version  : 2.0.5
  Build    : 17373eb129b3b05aa18ece963f8795d65ef8ea54

Activity

  1. bojanrajkovic commented on Jan 23, 2018

    @bojanrajkovic
    ContributorAuthor

    Using a custom server validation callback does work, but in order to hold on to the certificate after the request is complete (possibly even after the callback is complete), you must make a copy via Export.

    Unsurprisingly, creating a TcpClient, wrapping the stream in an SslStream, calling AuthenticateAsClient, and then checking the RemoteCertificate property also works.

  2. davidsh commented on Jan 23, 2018

    @davidsh
    Contributor

    ServicePoint is not supported on .NET Core. If you need to grab the server certificate using HttpClient, you can use the ServerCertificateCustomValidationCallback in HttpClientHandler:

    https://docs.microsoft.com/en-us/dotnet/api/system.net.http.httpclienthandler.servercertificatecustomvalidationcallback?view=netcore-2.0

    The server certificate is passed into this callback.

    cc: @karelz @geoffkizer

  3. bojanrajkovic commented on Jan 23, 2018

    @bojanrajkovic
    ContributorAuthor

    Yep, I figured that out, but it would be good to either document this, or make the ServicePoint-related code throw not supported exceptions on .NET Core. As it is, it seemed to be in a half-working state, because other parts of the SP were properly set, just not the certificate.

  4. karelz commented on Jan 23, 2018

    @karelz
    Member
  5. karelz commented on Oct 10, 2019

    @karelz
    Member

    Triage: We need the documentation. Mention alternative: HttpClient with certificate validation callback.

  6. transferred this issue fromdotnet/corefxon Jan 31, 2020
  7. added this to the 5.0 milestone on Jan 31, 2020
  8. modified the milestones: 5.0.0, 6.0.0 on Oct 9, 2020
  9. ghost locked as resolved and limited conversation to collaborators on Dec 18, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

area-System.Net.Httpdocumentation

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions