2016-11-22 7 views
0

Я работаю с клиентским приложением в Xamarin на Android, и мне нужны TLS 1.2 и NTLM.Как включить NTLM и TLS 1.2 с Xamarin на Android?

До сих пор я использовал регулярные 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 недавно пришел в Моно/Хамарин. Как недавняя сборка вы используете? http://tirania.org/blog/archive/2016/Sep-30.html – scotru

+0

Привет, Скотро, спасибо за ответ! Я использую Mono.Android runtime 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> 

В основном это говорит: Он поддерживает Basic и Digest аутентификации. Если есть схема аутентификации, которая не поддерживается в 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 не имеет ничего общего с базовыми API TLS. Ваше рукопожатие терпит неудачу с этим сервером, и поэтому вам придется исследовать причину. Мой ответ выше для использования NTLM в качестве вашей схемы аутентификации с новым AndroidClientHandler. –