抱歉,您的浏览器无法访问本站

本页面需要浏览器支持(启用)JavaScript


了解详情 >

开发者文档页面头部提示: “Starting 10 October 2017 developers must use TLS 1.1 or 1.2 when calling LinkedIn APIs. LinkedIn no longer supports TLS 1.0 for security reasons.” 中文翻译即是:“从2017年10月10日起,开发者必须使用 TLS1.1 或者 TLS1.2 请求 Linkedin API,因为安全的原因,LinkedIn 不再支持 TLS1.0”。

.Net Framework 4.5 中对 TLS(Transport Layer Security) 协议的枚举定义

namespace System.Net
{
  /// <summary>指定 Schannel 安全包支持的安全协议。</summary>
  [System.Flags]
  public enum SecurityProtocolType
  {
    /// <summary>指定由 Schannel 定义的系统默认安全协议。</summary>
    SystemDefault = 0,
    /// <summary>指定安全套接字层 (SSL) 3.0 安全协议。</summary>
    Ssl3 = 48, // 0x00000030
    /// <summary>指定传输层安全 (TLS) 1.0 安全协议。</summary>
    Tls = 192, // 0x000000C0
    /// <summary>指定传输层安全 (TLS) 1.1 安全协议。</summary>
    Tls11 = 768, // 0x00000300
    /// <summary>指定传输层安全 (TLS) 1.2 安全协议。</summary>
    Tls12 = 3072, // 0x00000C00
  }
}

如果项目是.Net Framework 4.5 版本可以在项目直接这样设置:

ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;

.Net Framework4.5 版本支持但默认并不是设置的这个协议,4.6 版本以上默认协议是 TLS 1.2,无需设置。

当前项目框架版本是 .Net Framework 4.0,但是这个框架版本不支持 TLS1.2SecurityProtocolType 是无法枚举 Tls12 这一项的,同时项目也并不准备升级到 4.5版本,还有解决办法吗?当然,我们依然可以枚举值来实现:

ServicePointManager.SecurityProtocol = (SecurityProtocolType) 3072;

这样产生的效果是一样的,在发送 POST 请求之前添加这一行就不会提示服务器请求异常了。

var httpClient = new HttpClient();
var body = new List<KeyValuePair<string, string>>();
……
ServicePointManager.SecurityProtocol = (SecurityProtocolType) 3072;
var reqResult = httpClient.PostAsync(tokenEndpoint, new FormUrlEncodedContent(body)).Result;

参考

TLS 1.2 and .NET Support: How to Avoid Connection Errors

Support for TLS System Default Versions included in the .NET Framework 3.5 on Windows Server 2012

评论