LoginSignup
10
6

More than 5 years have passed since last update.

VB.NETでTLS1.2対応

Last updated at Posted at 2018-09-05

WebサーバとHTTPS通信を行うVB.NETのアプリで、Webサーバを新しいものにリプレースしたらうまく接続できず、エラーを吐いていた。見慣れないエラーだ。リモートパーティってなんだ、僕も混ぜてもらえないか。

System.Net.WebException: 接続が切断されました: 送信時に、予期しないエラーが発生しました。。 ---> System.IO.IOException: リモート パーティがトランスポート ストリームを終了したため、認証に失敗しました。
   場所 System.Net.Security.SslState.StartReadFrame(Byte[] buffer, Int32 readBytes, AsyncProtocolRequest asyncRequest)
   場所 System.Net.Security.SslState.StartReceiveBlob(Byte[] buffer, AsyncProtocolRequest asyncRequest)
   場所 System.Net.Security.SslState.CheckCompletionBeforeNextReceive(ProtocolToken message, AsyncProtocolRequest asyncRequest)
   場所 System.Net.Security.SslState.ForceAuthentication(Boolean receiveFirst, Byte[] buffer, AsyncProtocolRequest asyncRequest)
   場所 System.Net.Security.SslState.ProcessAuthentication(LazyAsyncResult lazyResult)
   場所 System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
   場所 System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
   場所 System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
   場所 System.Net.TlsStream.ProcessAuthentication(LazyAsyncResult result)
   場所 System.Net.TlsStream.Write(Byte[] buffer, Int32 offset, Int32 size)
   場所 System.Net.ConnectStream.WriteHeaders(Boolean async)
   --- 内部例外スタック トレースの終わり ---
   場所 System.Net.HttpWebRequest.GetRequestStream(TransportContext& context)
   場所 System.Net.HttpWebRequest.GetRequestStream()

やれやれ。僕は検索した。

TLS1.2サポートするサーバとのHTTPS通信実装

どうやらTLS1.2環境とやらに対応する必要があるようだ。

対応

C#だとこう

ServicePointManager.SecurityProtocol |= SecurityProtocolType.Tls12;

全てのSecurityProtocolTypeを列挙してServicePointManager.SecurityProtocolに突っ込んでもいいが、単に追加するだけなら上記で足りるらしい。
へー。

VB.NETだとこう

ServicePointManager.SecurityProtocol = ServicePointManager.SecurityProtocol Or SecurityProtocolType.Tls12

スタートアップ時に呼び出されるMyApplication_Startup関数で呼んであげると以降接続失敗することはなくなった。
なお以下によると、C#でいう|=演算子はVBには無いらしい。まどろっこしい。

What is the equivalent of |= in Visual Basic?

おまけ

捜し物の過程で、IEでファイル>プロパティと開いていくとTLSバージョンが見られるというのを初めて知った。

.NET Framework で TLS1.1 および 1.2 を有効化する方法

10
6
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
10
6