据我所知,.NET 似乎有一个很大的限制,因为无法使用 C# 和 .NET 建立使用服务器名称指示 (SNI) 的 TLS 连接。我是否遗漏了什么或者我的理解是否正确?
有人知道我是否以及如何使用 OpenSSL.NET、libcurl.NET 或其他一些第 3 方 .NET 库建立 SNI 连接吗?非常感谢一些示例代码。
最佳答案
在我的 .Net 4.5 项目中,以下对于使用 SNI 的服务器失败:
var url = "https://www.somesite.com";
System.Net.WebClient client = new System.Net.WebClient();
client.Encoding = Encoding.UTF8;
var data = client.DownloadString(url);
但如果明确指定 TLS1.2 并在其前面加上前缀,它就可以工作:
System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
同样适用于webrequest:
WebRequest request = WebRequest.Create("https://www.somesite.com");
和 HttpRequestMessage:
var httpRequestMessage = new HttpRequestMessage(HttpMethod.Get, "https://www.google.com");
它们都需要将协议(protocol)显式设置为 TLS 1.2 才能与 SNI 服务器一起使用(这在较新的 .Net 版本中可能已更改)
关于c# - 来自 C# 的服务器名称指示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19971236/