我的应用程序连接到 Experian 服务器,而 Experian 将很快停止支持 TLS 1.0 和 TLS 1.1。所有使用 HTTPS 的连接都必须使用 TLS 版本 1.2。
我想对这个问题进行一些研究,并查看在 .NET 4.0 框架上使用 TLS 1.2 发送 HttpWebRequest
是否可行。
如果不行,我可能需要在 .NET 4.5 上创建一个 webservice
并调用其方法;如果可行,我就不必做任何事情。
是否有人已经遇到了这个问题?
我的应用程序连接到 Experian 服务器,而 Experian 将很快停止支持 TLS 1.0 和 TLS 1.1。所有使用 HTTPS 的连接都必须使用 TLS 版本 1.2。
我想对这个问题进行一些研究,并查看在 .NET 4.0 框架上使用 TLS 1.2 发送 HttpWebRequest
是否可行。
如果不行,我可能需要在 .NET 4.5 上创建一个 webservice
并调用其方法;如果可行,我就不必做任何事情。
是否有人已经遇到了这个问题?
是的,它支持TLS,但您必须在ServicePointManager
上明确设置TLS版本。只需在调用Experian之前的任何时间(在同一应用程序域中)运行此代码即可:
System.Net.ServicePointManager.SecurityProtocol = System.Net.SecurityProtocolType.Tls12
查看@iignatov的回答,了解您在框架 v4.0 中必须执行的操作。我的代码适用于4.5+。
我在将PayPal集成到旧应用程序中时也遇到了同样的问题,并找到了以下解决方法,适用于.NET 4.0,似乎能解决这个问题:
ServicePointManager.Expect100Continue = true;
ServicePointManager.SecurityProtocol = (SecurityProtocolType)3072;
ServicePointManager.DefaultConnectionLimit = 9999;
基本上的解决方法是直接为TLS 1.2分配端口。
所有的功劳归功于在CodeProject的评论者。
ServicePointManager.SecurityProtocol = (SecurityProtocolType)3072
做到了它的作用。非常感谢! - Renan Araújoiignatov的答案的VB.NET翻译:
ServicePointManager.Expect100Continue = True
ServicePointManager.SecurityProtocol = CType(3072, SecurityProtocolType)
ServicePointManager.DefaultConnectionLimit = 9999
我用这种方法解决了问题。
string url = "https://api.foursquare.com/v2/blablabla...";
var request = (HttpWebRequest)WebRequest.Create(url);
ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3 | SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls;
var response = (HttpWebResponse)request.GetResponse();
var responseString = new StreamReader(response.GetResponseStream()).ReadToEnd();
最初的回答:
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls | (SecurityProtocolType)768 | (SecurityProtocolType)3072;
使用针对 Framework 4.5 的应用程序(但安装了 SDK 4.8),上述解决方案均不适用于我。
我在注册表中创建了一个名为 SchUseStrongCrypto
的 DWORD 键。
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\.NETFramework\v4.0.30319
并且
HKEY_LOCAL_MACHINE\SOFTWARE\WOW6432Node\Microsoft\.NETFramework\v4.0.30319
具有值为1。
因此,似乎不需要更改ServicePointManager.SecurityProtocol
。
不行,
正如来源所述,
“HttpWebRequest 类仅支持 HTTP 的版本 1.0 和 1.1。将 ProtocolVersion 设置为其他版本会引发异常。”
Source: learn.microsoft.com
ServicePointManager.Expect100Continue = true;
ServicePointManager.SecurityProtocol = (SecurityProtocolType)3072;
这在我的 .net 3.5 上运行良好
FrameWork 4.0 不支持 TLS 1.1 或 1.2 但您可以通过从 Nuget 管理器下载 Rebex.Http 来解决此问题。
Rebex.Licensing.Key = "..."; //Lisans Number
var creator = new HttpRequestCreator();
creator.Register();
WebRequest request = WebRequest.Create("https://www.test.com");
request.Method = "POST";
request.Headers.Add("utsToken", txtToken.Text);
request.ContentType = "application/json";
request.Method = "POST";
using (var streamWriter = new StreamWriter(request.GetRequestStream()))
{
string json = "{\"VRG\":\"test\"}";
streamWriter.Write(json);
streamWriter.Flush();
streamWriter.Close();
}
var httpResponse = (WebResponse)request.GetResponse();
using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
{
var result = streamReader.ReadToEnd();
txtSonuc.Text += result;
}
SecurityProtocolType
选项可用,即Ssl3
和Tls
。它没有Tls12
。 - geneSecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls10
。 - Crowcoder