我们有一些 .NET 代码调用位于 Azure Frontdoor 后面的 Azure 网站。该代码使用 HttpClient 以非常简单的方式进行调用。通过 Azure Frontdoor 调用我们的站点时,我们开始收到以下错误。
现有连接被远程主机强制关闭
xxxx.azurefd.net
结尾的内容都会生成此错误。以下代码会产生问题。
static async Task Main(string[] args)
{
if (ServicePointManager.SecurityProtocol.HasFlag(SecurityProtocolType.Tls12) == false)
{
ServicePointManager.SecurityProtocol = ServicePointManager.SecurityProtocol | SecurityProtocolType.Tls12;
}
ServicePointManager.SecurityProtocol |= SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls;
Console.WriteLine($"Protocol: {ServicePointManager.SecurityProtocol}");
var client = new HttpClient { BaseAddress = new Uri("https://anything.azurefd.net") };
try
{
var result = await client.GetAsync("someroute");
Console.WriteLine(await result.Content.ReadAsStringAsync());
}
catch(Exception e)
{
while(e != null)
{
Console.WriteLine(e.Message);
e = e.InnerException;
}
}
Console.ReadLine();
}
提前致谢。
最有可能的是,使用 HttpClient 访问 Azure Frontdoor 时的连接问题与 TLS 配置或兼容性有关。在初始化 HttpClient 之前,您应该尝试将 ServicePointManager.SecurityProtocol 专门设置为 SecurityProtocolType.Tls12。
我想尝试一下:
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
var client = new HttpClient { BaseAddress = new Uri("https://anything.azurefd.net") };