2017-12-05 20 views
1

私は、cURLで使用するサンプルコードを提供するサードパーティとの統合に取り組んでいます。cURLをRestSharpに変換する(SSL/TLSセキュアなチャネルを作成できませんでした)

curl -k -d "grant_type = client_cert" --basic -u "myUserName:myPassword" -H "Content-Type:application/x-file"次のコマンドを使用して、cURLを使用して正常に接続できました。 www-form-urlencodedで返します」--cert C:\ certsの\ MYCERT.PEM https://vendorUrl/method

cURLのドキュメントを読んで、私は、これはすべてのスイッチがに変換するものであると考えている:

  • -k:安全でありません(接続が安全でなくても進んでください)
  • -d:data。またこれがPOSTであることを示します。 --basic
  • :ベーシック認証
  • -uユーザ名/パスワード
  • -H追加のヘッダー
  • --cert:(!)私は」さまざまなヒントを試してみました

合格証明書オンラインで見つかったので、不要なコード行がいくつかあります。私は、コードコメントでCURLをC#に変換した箇所を示すようにしました。

 //Not sure if these are needed or not. 
     ServicePointManager.Expect100Continue = true; 
     ServicePointManager.CheckCertificateRevocationList = false; 

     //allow every possible protocol for now just to eliminate the protocol as the source of the problem 
     ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12 | SecurityProtocolType.Ssl3; 

     //this should be the equivalent of cURL's "-k" (insecure) switch. 
     ServicePointManager.ServerCertificateValidationCallback += (sender, certificate, chain, sslPolicyErrors) => true; 

     var certificatePath = ConfigurationManager.AppSettings.GetValue("myPath"); 
     var clientKey = "myKey"; 
     var clientSecret = "mySecret"; 

     var url = "https://vendorUrl/method"; 

     //create the request 
     var request = new RestRequest(Method.POST); 

     ////this should be the equivalent of cURL's -d (data) switch. no idea if this is done correctly. I have also tried adding this as "AddJsonBody()" 
     var body = "grant_type=client_cert"; 
     request.AddBody(body); 

     //this should be the equivalent of cURL's "-H" (header) switch 
     request.AddHeader("Content-Type", "application/x-www-form-urlencoded"); 

     //Import certificate 
     var certificates = new X509Certificate(); 
     certificates.Import(certificatePath); 

     var client = new RestClient 
     { 
      BaseUrl = new Uri(url), 
      //this should be the equivalent of cURL's "--cert" switch 
      ClientCertificates = new X509CertificateCollection { certificates }, 
      //this should be the equivalent of cURL's "--basic" (Basic Auth) switch and the "-u" switch 
      Authenticator = new HttpBasicAuthenticator(clientKey, clientSecret) 
     }; 

     var response = client.Execute(request); 

私が取得エラーメッセージが恐ろしい「要求が中止されました:SSL/TLSのセキュリティで保護されたチャネルを作成できませんでした。」です

また、ここで説明するようにトレースログを追加しました:The request was aborted: Could not create SSL/TLS secure channel

私は本当に、トレースの出力を解析する方法を知りませんが、トレースの最後のメッセージのいずれかが問題のようになります。

System.Net情報:0:[9232] InitializeSecurityContextの(インバッファ数= 2、アウトバッファ長= 0、返されるコード= IllegalMessage)。

+0

RestSharpは、SSLをサポートするために何もしません。 'WebRequest'を作成するだけです。ですから、WebRequestで動作させることができればRestSharpでも使えます。これはあまり役に立ちませんが、それでもなおわかります。 –

答えて

0

私の場合、これを動作させるには、ローカルコンピュータ/サーバーに証明書をインストールする必要がありました。証明書がインストールされていないcURLが動作する理由はわかりません。

私がインストールしなければならなかった証明書は、拡張子が.p12でした。したがって、コードでは.pemファイルをロードしました。このファイルは、.p12ファイルから派生したファイルです。私は理由を理解すると主張していない。

のHttpClientに切り替えることで、私は追加することにより、.PEMファイルをロード避けることができた:

  var handler = new WebRequestHandler(); 
      handler.ClientCertificateOptions = ClientCertificateOption.Automatic; 
      var httpClient = new HttpClient(handler); 

私はClientCertificateOptionsにRestSharp/WebRequestクラスと同等を見つけることができませんでした。

関連する問題