【发布时间】:2017-08-03 18:31:56
【问题描述】:
我需要将 JSON 数据发布到 TLS 1.2 端点。我希望在 App.config 中指定 SecurityProtocol,而不是在源代码中进行硬编码,并且不想将机器的注册表设置为禁用 TLS 1.1。
如果您未指定 SecurityProtocol,则使用底层操作系统协议,但它们似乎默认为最不安全而不是最安全。因为我从机器上运行了多个服务,所以我无法将操作系统设置为仅使用 TLS1.2,但我仍然希望这个特定的客户端使用 TLS 1.2,并且当 TLS 1.3 出现时,可以通过应用程序特定的配置对其进行修改。
这个问题通过代码解释了如何做到这一点:How to specify SSL protocol to use for WebClient class
这个问题解释了如何通过整个机器的注册表设置来做到这一点:Are there .NET implementation of TLS 1.2?
// this is what I want to avoid
System.Net.ServicePointManager.SecurityProtocol = System.Net.SecurityProtocol.Tls12;
System.Net.HttpWebRequest request = (System.Net.HttpWebRequest)System.Net.HttpWebRequest.Create(url);
using (System.IO.StreamWriter sw = new System.IO.StreamWriter(request.GetRequestStream()))
{
sw.write(json);
}
System.Net.HttpWebResponse response = (System.Net.HttpWebResponse)request.GetResponse();
using System.IO.StreamReader sr = new System.IO.StreamReader(response.GetResponseStream()))
{
content = sr.ReadToEnd();
}
目前我的 App.config 中没有此客户端的任何内容,但这就是我想要更改的内容。
我发现system.serviceModel里面有一个sslStreamSecurity元素,但我相信这是给ServiceReferences的,不是普通的HttpWebRequest。我相信 system.net 涵盖了这一点,但我找不到等价物。
<system.serviceModel>
<bindings>
<customBinding>
<binding name="myBinding">
<sslStreamSecurity sslProtocls="Tls12">
</binding>
</customBinding>
</bindings>
<client>
<endpoint address="https://myserver.com" binding="customBinding" bindingConfiguration="myBinding" name="myEndpoint" />
</client>
</system.ServiceModel>
我对使用 HttpWebRequest/HttpWebResponse 以外的东西持相当开放的态度,但不想安装第三方包。我从System.Net.Http.HttpClient 开始,它似乎比 HttpWebRequest 更新,但很快遇到了同样的问题。
【问题讨论】:
标签: .net httpwebrequest webclient app-config dotnet-httpclient