2017-09-25 83 views
0

我想从Xamarin.Forms使用TLS1.2发送POST事务,但我看到他们到达服务器作为TLS 1.1。不能发送TLS 1.2与Xamarin.Forms和VisualStudio 2017

我已经配置的Android选项:

HttpClient的impletemtation为的Android

SSL/TLS实现为本地TLS 1.2+

我实施和VisualStudio的2017年执行,并使用模拟器中的Android 6.0。

关于代码,我设置一些环境变量:

using (HttpClient client = new HttpClient()) 
    or 
    using (HttpClient client = new HttpClient(new NativeMessageHandler())) 
    or 
    using (HttpClient client = new HttpClient(new Xamarin.Android.Net.AndroidClientHandler())) 
    { 
    try 
    { 
    HttpResponseMessage responseHttp = await client.PostAsync(new Uri(new Uri(Constants.ApiBaseUrl), "authorize"), content); 
    ... 

凡Constants.ApiBaseUrl包含一个URL以https:// <>格式

System.Environment.SetEnvironmentVariable("MONO_TLS_PROVIDER‌​", "btls"); 
System.Environment.SetEnvironmentVariable("XA_TLS_PROVIDER‌​", "btls"); 
System.Environment.SetEnvironmentVariable("XA_HTTP_CLIENT_HANDLER_TYPE", "Xamarin.Android.Net.AndroidClientHandler"); 
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12; 

最后,POST与发送。

的问题是,当POST发送我没有任何异常,但在我的服务器我使用Wireshark看到作为交易:

enter image description here 我也曾尝试以其他方式使用:

HttpWebRequest httpWebRequest = WebRequest.CreateHttp(new Uri(new Uri(Constants.ApiBaseUrl), "authorize")); 
    httpWebRequest.Method = "POST"; 
    httpWebRequest.Credentials = CredentialCache.DefaultNetworkCredentials; 
    Stream sw = httpWebRequest.GetRequestStream(); 
    sw.Write(contentByte, 0, contentByte.Length); 
    HttpWebResponse httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse(); 

带和不使用我自己的证书:

httpWebRequest.ClientCertificates = cryptoSvc.x509HostCertificates; 

在这种情况下,如果我用

ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12; 

我得到的异常

RestService-SendJsonDataAsync ERROR: Error: SecureChannelFailure 
     (**Ssl error:100000f0:SSL routines:OPENSSL_internal:UNSUPPORTED_PROTOCOL** 
     at /Users/builder/jenkins/workspace/xamarin-android/xamarin-android/external/mono/external/boringssl/ssl/handshake_client.c:808) 

没有该行它也为到达TLS1.1。

有没有人有任何想法或建议我的情况有什么不对?

非常感谢您的时间和帮助。

回答

1

我相信在Android 6.0,TLS 1.2是默认启用:

https://developer.android.com/reference/javax/net/ssl/SSLSocket.html

但是,你有BTLS配置和Native TLS 1.2,我认为这是矛盾的。 BTLS是乏味的TLS实现,旨在为旧版Android提供TLS 1.2支持。我认为你可以安全地删除这些行。

我首先确认服务器允许的安全性,如果可能的话,服务器将HTTPS连接限制为仅限于TLS 1.2。

如果您想尝试仅强制使用TLS 1。在客户端2个连接,看看这个答案(注意,这是在Java中,但过程是相同的):

How to set TLS version on apache HttpClient

+0

感谢您发表评论@DanielMaclean,我想没有那些线,但结果相同。另一方面,我的服务器现在允许两个连接。 – santiPipes

+0

你可以尝试禁用服务器上的1.1吗? – LewisT

+0

我不能,服务器必须允许这两个连接。我实际上使用TLS1.1和TLS1.2连接付款终端到这台服务器,没有麻烦。 – santiPipes