小编Car*_*ans的帖子

通过App.config为System.Net.HttpWebRequest指定SSL / TLS

我需要将JSON数据发布到TLS 1.2端点。我想在App.config中指定SecurityProtocol,而不是在源中进行硬编码,并且不想将计算机的注册表设置为禁用TLS 1.1。

如果未指定SecurityProtocol,则使用底层OS协议,但默认情况下它们似乎是最不安全的,而不是最安全的。因为我有从计算机运行的多种服务,所以不能将操作系统设置为仅使用TLS1.2,但是我仍然希望该特定客户端使用TLS 1.2,并且当TLS 1.3推出时,可以通过应用程序特定的配置对其进行修改。

这个问题解释了如何通过代码做到这一点:如何为WebClient类指定SSL协议

这个问题说明了如何通过整个计算机的注册表设置来做到这一点:是否存在TLS 1.2的.NET实现?

// 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();
}
Run Code Online (Sandbox Code Playgroud)

我的客户端当前在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>
Run Code Online (Sandbox Code Playgroud)

我很愿意使用HttpWebRequest / HttpWebResponse以外的其他方式,但希望远离安装第三方软件包。我开始寻找一条System.Net.Http.HttpClient比HttpWebRequest更新的路径,但很快遇到了同样的问题。

.net app-config webclient httpwebrequest dotnet-httpclient

6
推荐指数
2
解决办法
6484
查看次数