title: Specifying SSL/TLS for System.Net.HttpWebRequest through App.config
body: <p>I need to POST JSON data to a TLS 1.2 Endpoint. I would like to have the SecurityProtocol specified in the App.config instead of being hardcoded in the source and do not want to set the registry of the machine to disable TLS 1.1.</p>
  
  <p>If you do not specify the SecurityProtocol the underlying OS protocols are used but they seem to default to the least secure instead of the most secure. Because I have multiple services running from the machine I cannot set the OS to only use TLS1.2, but I still want this specific client to use TLS 1.2, and when TLS 1.3 comes out be able to modify it through application specific configuration.</p>
  
  <p>This question explains how to do it via code: <a href="https://stackoverflow.com/questions/30491716/how-to-specify-ssl-protocol-to-use-for-webclient-class">How to specify SSL protocol to use for WebClient class</a></p>
  
  <p>This question explains how to do it via Registry settings for the entire machine: <a href="https://stackoverflow.com/questions/4137106/are-there-net-implementation-of-tls-1-2">Are there .NET implementation of TLS 1.2?</a></p>
  
  <pre><code>// 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();
  }
  </code></pre>
  
  <p>I do not have anything in my App.config for this client currently but that is what I would like to change. </p>
  
  <p>I have found that there is a sslStreamSecurity element inside system.serviceModel, but I believe this is for ServiceReferences, not normal HttpWebRequest. I believe that is covered under system.net but I can't find an equivalent. </p>
  
  <pre><code>&lt;system.serviceModel&gt;
    &lt;bindings&gt;
      &lt;customBinding&gt;
        &lt;binding name="myBinding"&gt;
          &lt;sslStreamSecurity sslProtocls="Tls12"&gt;
        &lt;/binding&gt;
      &lt;/customBinding&gt;
    &lt;/bindings&gt;
    &lt;client&gt;
      &lt;endpoint address="https://myserver.com" binding="customBinding" bindingConfiguration="myBinding" name="myEndpoint" /&gt;
    &lt;/client&gt;
  &lt;/system.ServiceModel&gt;
  </code></pre>
  
  <p>I am fairly open to using something other than HttpWebRequest/HttpWebResponse but would like to stay away from installing third party packages. I started down a path with <code>System.Net.Http.HttpClient</code> which seems newer than HttpWebRequest but quickly ran into the same issues.</p>