2017-08-03 19 views
0

JSONデータをTLS 1.2エンドポイントにPOSTする必要があります。ソースにハードコードされているのではなく、TLS 1.1を無効にするためにマシンのレジストリを設定するのではなく、App.configでSecurityProtocolを指定したいと思います。App.configを通じてSystem.Net.HttpWebRequestのSSL/TLSを指定する

SecurityProtocolを指定しない場合は、基盤となるOSプロトコルが使用されますが、最も安全性の高いセキュリティではなく、最も安全性が低いと思われます。私はマシンから複数のサービスを実行しているため、TLS1.2のみを使用するようにOSを設定することはできませんが、この特定のクライアントに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(); 
} 

私は私のアプリでは何もありません。現在このクライアントの.configファイルを変更したいのですが。

system.serviceModelの中にsslStreamSecurity要素があることがわかりましたが、これは通常のHttpWebRequestではなくServiceReferencesのためだと思います。私はそれが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よりも新しいように見えましたが、すぐに同じ問題に遭遇しました。

答えて

0

今のところ私はSystem.Net.ServicePointManager.SecurityProtocolに設定する列挙値を指定する整数値をApp.configに入れました。私はまだこれを行うよりエレガントな方法があり、他の答えを楽しみにしているかもしれないと思います。私はhttps://stackoverflow.com/a/35325333/5221761

int securityProtocol = Convert.ToInt32(ConfigurationManager.AppSettings["SecurityProtocol"]); 

try 
{ 
    System.Net.ServicePointManager.SecurityProtocol = (System.Net.SecurityProtocolType)securityProtocol; 
} 
catch(Exception ex) 
{ 
    Console.WriteLine("Could not setup SecurityProtocol. Try a different integer value: " + ex.Message); 
    foreach (System.Net.SecurityProtocolType protocolType in Enum.GetValues(typeof(System.Net.SecurityProtocolType))) 
    { 
     Console.WriteLine(string.Foramt("SecurityProtocol: {0} - {1}", protocolType.ToString(), (int)protocolType)); 
    } 
} 

App.configファイルで、この方向に案内されました:

<appSettings> 
    <add key="SecurityProtocol" value="3072" /> 
</appSettings> 
関連する問題