2016-11-22 93 views
0

我正在使用Android上的Xamarin客戶端應用程序,我需要TLS 1.2和NTLM。如何在Android上使用Xamarin啓用NTLM和TLS 1.2?

到目前爲止,我一直在使用常規System.Net.HttpClientHandler和它工作得很好 - 它看起來像這樣:

new System.Net.Http.HttpClientHandler() 
     { 
      Credentials = credentials 
     }; 

但現在我有一個新的客戶,我需要TLS 1.2。所以我做了針對Android的代碼:

new Xamarin.Android.Net.AndroidClientHandler() 
     { 
      Credentials = credentials 
     }; 

與環境變量:

XA_HTTP_CLIENT_HANDLER_TYPE=Xamarin.Android.Net.AndroidClientHandler 

現在,這種AndroidClientHandler工作儘可能的證書去。但我也需要NTLM才能工作。對我來說,似乎AndroidClientHandler只支持Basic和Digest身份驗證方案(請參閱Xamarin.Android.Net.AuthenticationScheme)。

我也嘗試過使用ModernHttpClient,但在我看來,它使用Mono的方式與System.Net.Http.HttpClientHandler一樣,所以TLS 1.2在那裏也不起作用。

在我看來,這應該是一個很常見的情況,但我仍然無法在網上找到相關示例。我希望我只是錯過了一些明顯的東西。你們怎麼解決這個問題?

+0

最近TLS 1.2來到Mono/Xamarin。您最近使用的構建版本是多久? http://tirania.org/blog/archive/2016/Sep-30.html – scotru

+0

嗨scotru,並感謝您的答案!我正在使用Mono.Android運行時4.0.30319。我想這就是我用最新版本的Xamarin獲得的。 – Christian

+0

我不認爲Miguel所引用的更新已經將它變成了Mono.Android運行時版本。它看起來像ModernHttpClient應該與TLS 1.2一起工作。 https://wolfprogrammer.com/2016/08/23/enabling-tls-1-2-in-xamarin-forms/ – scotru

回答

0

我相信這將是有益的你讀通過:

https://github.com/xamarin/xamarin-android/blob/0c3597869bc4493895e755bda8a26f778e4fe9e0/src/Mono.Android/Xamarin.Android.Net/AndroidClientHandler.cs#L40-L56

/// <para> 
/// The class supports pre-authentication of requests albeit in a slightly "manual" way. Namely, whenever a request to a server requiring authentication 
/// is made and no authentication credentials are provided in the <see cref="PreAuthenticationData"/> property (which is usually the case on the first 
/// request), the <see cref="RequestNeedsAuthorization"/> property will return <c>true</c> and the <see cref="RequestedAuthentication"/> property will 
/// contain all the authentication information gathered from the server. The application must then fill in the blanks (i.e. the credentials) and re-send 
/// the request configured to perform pre-authentication. The reason for this manual process is that the underlying Java HTTP client API supports only a 
/// single, VM-wide, authentication handler which cannot be configured to handle credentials for several requests. AndroidClientHandler, therefore, implements 
/// the authentication in managed .NET code. Message handler supports both Basic and Digest authentication. If an authentication scheme that's not supported 
/// by AndroidClientHandler is requested by the server, the application can provide its own authentication module (<see cref="AuthenticationData"/>, 
/// <see cref="PreAuthenticationData"/>) to handle the protocol authorization.</para> 
/// <para>AndroidClientHandler also supports requests to servers with "invalid" (e.g. self-signed) SSL certificates. Since this process is a bit convoluted using 
/// the Java APIs, AndroidClientHandler defines two ways to handle the situation. First, easier, is to store the necessary certificates (either CA or server certificates) 
/// in the <see cref="TrustedCerts"/> collection or, after deriving a custom class from AndroidClientHandler, by overriding one or more methods provided for this purpose 
/// (<see cref="ConfigureTrustManagerFactory"/>, <see cref="ConfigureKeyManagerFactory"/> and <see cref="ConfigureKeyStore"/>). The former method should be sufficient 
/// for most use cases, the latter allows the application to provide fully customized key store, trust manager and key manager, if needed. Note that the instance of 
/// AndroidClientHandler configured to accept an "invalid" certificate from the particular server will most likely fail to validate certificates from other servers (even 
/// if they use a certificate with a fully validated trust chain) unless you store the CA certificates from your Android system in <see cref="TrustedCerts"/> along with 
/// the self-signed certificate(s).</para> 

這基本上說:它支持BasicDigest認證。如果服務器請求的AndroidClientHandler中不支持認證方案,則應用程序可以提供它自己的認證模塊來處理協議授權。

然後,我們可以看到,RequestedAuthentication屬性將列出的關於服務器:支持的每個方案

https://github.com/xamarin/xamarin-android/blob/0c3597869bc4493895e755bda8a26f778e4fe9e0/src/Mono.Android/Xamarin.Android.Net/AndroidClientHandler.cs#L116-L124

/// <summary> 
/// If the website requires authentication, this property will contain data about each scheme supported 
/// by the server after the response. Note that unauthorized request will return a valid response - you 
/// need to check the status code and and (re)configure AndroidClientHandler instance accordingly by providing 
/// both the credentials and the authentication scheme by setting the <see cref="PreAuthenticationData"/> 
/// property. If AndroidClientHandler is not able to detect the kind of authentication scheme it will store an 
/// instance of <see cref="AuthenticationData"/> with its <see cref="AuthenticationData.Scheme"/> property 
/// set to <c>AuthenticationScheme.Unsupported</c> and the application will be responsible for providing an 
/// instance of <see cref="IAndroidAuthenticationModule"/> which handles this kind of authorization scheme 
/// (<see cref="AuthenticationData.AuthModule"/> 
/// </summary> 

這就告訴我們,如果返回Unsupported作爲我們AuthenticationScheme,然後我們需要提交我們自己的IAndroidAuthenticationModule來應對挑戰。

這裏是AuthenticationScheme的枚舉:

https://github.com/xamarin/xamarin-android/blob/24f2aec113857b5c583e14959b9af08ad45b22b1/src/Mono.Android/Xamarin.Android.Net/AuthenticationScheme.cs

namespace Xamarin.Android.Net 
{ 
    /// <summary> 
    /// Authentication schemes supported by <see cref="AndroidClientHandler"/> 
    /// </summary> 
    public enum AuthenticationScheme 
    { 
     /// <summary> 
     /// Default value used in <see cref="AuthenticationData.Scheme"/> 
     /// </summary> 
     None, 

     /// <summary> 
     /// <see cref="AndroidClientHandler"/> doesn't support this scheme, the application must provide its own value. See <see cref="AuthenticationData.Scheme"/> 
     /// </summary> 
     Unsupported, 

     /// <summary> 
     /// The HTTP Basic authentication scheme 
     /// </summary> 
     Basic, 

     /// <summary> 
     /// The HTTP Digest authentication scheme 
     /// </summary> 
     Digest 
    } 
} 

因此,我們必須通過具體的實現擴展這個接口來實現自定義IAndroidAuthenticationModule

https://github.com/xamarin/xamarin-android/blob/24f2aec113857b5c583e14959b9af08ad45b22b1/src/Mono.Android/Xamarin.Android.Net/IAndroidAuthenticationModule.cs

然後你會將該實現傳遞給AuthenticationData.AuthModule屬性:

https://github.com/xamarin/xamarin-android/blob/24f2aec113857b5c583e14959b9af08ad45b22b1/src/Mono.Android/Xamarin.Android.Net/AuthenticationData.cs#L37

你會再傳遞到主客戶端的PreAuthenticationData財產。

https://github.com/xamarin/xamarin-android/blob/0c3597869bc4493895e755bda8a26f778e4fe9e0/src/Mono.Android/Xamarin.Android.Net/AndroidClientHandler.cs#L113

我希望這有助於!

+0

什麼關於Xamarin.Forms? Droid版本無法連接到TLS 1.2網站。 加載圖片:獲取https://randomuser.me/api/portraits/med/men/85.jpg流的錯誤:System.Net.WebException:錯誤:SecureChannelFailure(驗證或解密失敗。)---> System.IO.IOException:驗證或解密失敗。 ---> System.IO.IOException:認證或解密失敗。 ---> Mono.Security.Protocol.Tls.TlsException:認證或解密失敗。 –

+0

Xamarin Forms與底層的TLS API無關。您的握手與此服務器失敗,因此您必須調查原因。我上面的答案是使用NTLM作爲您的身份驗證方案與新的AndroidClientHandler。 –