diff --git a/src/Mono.Android/Xamarin.Android.Net/AndroidMessageHandler.cs b/src/Mono.Android/Xamarin.Android.Net/AndroidMessageHandler.cs index b2eb1d0eae6..94a34ad15b9 100644 --- a/src/Mono.Android/Xamarin.Android.Net/AndroidMessageHandler.cs +++ b/src/Mono.Android/Xamarin.Android.Net/AndroidMessageHandler.cs @@ -507,13 +507,9 @@ or ObjectDisposedException "Last-Modified" }; - static readonly List authModules = new List { - new AuthModuleBasic (), - new AuthModuleDigest () - }; - CookieContainer? _cookieContainer; DecompressionMethods _decompressionMethods; + IAndroidAuthenticationModule []? authModules; bool disposed; bool started; @@ -759,7 +755,20 @@ public int MaxAutomaticRedirections /// /// /// The pre authentication data. - public AuthenticationData? PreAuthenticationData { get; set; } + public AuthenticationData? PreAuthenticationData { + get; + set { + // Keep these constructor references in the setter so apps that never configure pre-authentication can trim the built-in modules. + if (value != null && authModules == null) { + authModules = [ + new AuthModuleBasic (), + new AuthModuleDigest () + ]; + } + + field = value; + } + } /// /// If the website requires authentication, this property will contain data about each scheme supported @@ -1768,7 +1777,7 @@ void HandlePreAuthentication (HttpURLConnection httpConnection) return; } - var auth = data.Scheme == AuthenticationScheme.Unsupported ? data.AuthModule : authModules.Find (m => m?.Scheme == data.Scheme); + var auth = data.Scheme == AuthenticationScheme.Unsupported ? data.AuthModule : authModules?.FirstOrDefault (m => m.Scheme == data.Scheme); if (auth == null) { if (Logger.LogNet) Logger.Log (LogLevel.Info, LOG_APP, $"Authentication module for scheme '{data.Scheme}' not found. No authentication will be performed");