Server Name Indication from C#

前端 未结 2 1121

As far as I can tell, there seems to be a big limitation in .NET in that there is no way using C# and .NET to make an TLS connection that uses Server Name Indication (SNI).

2条回答
  •  被撕碎了的回忆
    2021-01-18 08:20

    This is a fairly old post but still this answer might help some people, at least it cost me some days.

    .NET Framework does support the Server Name Indication by default. (Tested on 4.5.1 but I guess it's same at least for .NET 4.5+)

    A short example:

    HttpResponseMessage response;
    var httpRequestMessage = new HttpRequestMessage(HttpMethod.Get, "https://www.google.com");
    
    var handler = new HttpClientHandler
    {
        CookieContainer = new CookieContainer()
    };
    using (var client = new HttpClient(handler))
    {
        response = client.SendAsync(httpRequestMessage).Result;
    }
    

    This is a very standard way to create a GET request within C#. You will see, this example does run using, in my case, TLS 1.2 with SNI. If you use Wireshark to see the actual packages which are sent, you will see a Client Hello with the Server Name Indication set to www.google.com.

    An issue we ran into: The SNI tag is set by the .NET Framework (or Schannel by Windows, not sure) based on the URL passed in the constructor of HttpRequestMessage. If you know initialize the request based on some URL (for example https://www.google.com) and later on you switch the RequestUri OR the Host header, the SNI tag will still be created based on the original url URL. This might be the case for example if you pass through a request and you remap all original headers to the newly created request.

提交回复
热议问题