You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

.NET 6 应用强制使用 TLS 1.2 的方法咨询

强制.NET 6.0应用程序使用TLS 1.2的方法

有几种简单的方式可以实现,无需切换到.NET Framework 4.7/4.8:

1. 代码全局设置

在应用启动初期(比如Program.cs的入口处)添加以下代码,全局强制所有HTTP请求使用TLS 1.2:

System.Net.ServicePointManager.SecurityProtocol = System.Net.SecurityProtocolType.Tls12;

2. 针对特定HttpClient配置

如果只需要某一个HttpClient实例使用TLS 1.2,可以通过HttpClientHandler单独配置:

var handler = new HttpClientHandler();
handler.SslProtocols = System.Security.Authentication.SslProtocols.Tls12;
var httpClient = new HttpClient(handler);

3. 配置文件全局生效

在appsettings.json中添加配置,让所有基于SocketsHttpHandler的HTTP请求默认使用TLS 1.2(.NET 6默认使用该Handler):

{
  "System.Net.Http.SocketsHttpHandler": {
    "SslOptions": {
      "EnabledSslProtocols": 3072
    }
  }
}

注:3072是SslProtocols.Tls12对应的整数枚举值。

4. 环境变量配置

通过设置环境变量实现,适合部署时灵活调整,无需修改代码:

DOTNET_SYSTEM_NET_HTTP_SOCKETSHTTPHANDLER_SSLOPTIONS_ENABLEDSSLPROTOCOLS=3072

内容的提问来源于stack exchange,提问作者xguysOG

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.05 11:50:22