Quick Links

Problem with HttpClient after updating Xamarin.iOS

I'm currently investigating some strange behavior I see when using the System.Net.Http.HttpClient.

We have a REST API with NTLM Authentication enabled running on our server and our apps (iOS, Android, WP) get and POST data to this API. This solutions has been working fine for many months but last week we updated Xamarin Studio and Xamarion.iOS to the latest version and since then the POST (only the POSTs, GETs are working perfectly) stopped to work and the server returns 401 UNAUTHORIZED. This error is only happening on iOS, the Android and Windows Phone versions are still work perfectly with the same code... All of them use the same Service to communicate with the API.

It's super weird and it's driving us crazy. We tried to force old versions of the Microsoft.Net.Http, to use a package called ModernHttpClient... and nothing worked. We don't remember the old version of Xamarion.iOS but it was quite old... around 8.6, and now we are using the latest.

In the following lines you'll find the functions we use to perform the communications. First the GET function, that works perfectly and then the POST function that only doesn't work on iOS and was working perfectly on the previous version.

If only iOS is not working, I would look into what details are getting passed to the PCL. 401 unauthorized means you're missing the authentication details or they are wrong. I am not convinced it's a Xamarin or iOS problem. Just check that someone didn't change the iOS integration with your PCL.

I have a similar issue. Previously working POST commands are no longer working with NTLM authentication. I'm the only developer working on this app and have not changed anything about the PCL integration in my iOS app. In my case the only change made was getting the latest version of Xamarin.iOS. I happen to have an older MBP that I just replaced and on there everything still works fine, but it's not yet updated.

We are exactly in the same situation MatthewNoel... we installed Xamarin 5.8 and Xamarin.iOS 8.8.0.2 and it's working again!

We have investigated a little bit more using Fiddler: the working version negotiates the NTLM in the proper way: 3 calls to the same URL and the third gets a 200. However, compiled using the latest version of Xamarin.iOS the first call is done to the host, ignoring the rest of the URL. I can't understand why this changed...

(This might not be related, please disregard if it's not.) I am currently seeing the "Authorization" header on System.Net.HttpWebRequests being discarded. This happens with webRequest.PreAuthenticate = true or false, as soon as I change the header from "Authorization" to "ZAuthorization" it sends it straight down.

They said this would be fixed in SR3 of the latest Xamarin.iOS, so I assume that corresponds with 8.10.3? I don't do any Android dev, so I'm not sure what version you're on there, but it was apparently fixed in Mono 4.2, whenever they roll that in, you should get the fix, assuming it's the same issue+fix.

@UrmilSetia ModernHttpClient is also broken for NTLM, even after rolling back to Xamarin.iOS 8.9.1.3. I tried rolling back to a previous version of ModernHttpClient as well, but couldn't find one that worked.

I updated my environment to Windows 10 + VS2015 and the latest Xamarin tooling, after using the old Xamarin.IOS version for some time.. And now the problem returned.. Any ideas on when this bugfix is going to be released?

I didn't want to do an other downgrade, so I took a deepdive into this.. For me the problem was related to using Basic authentication with HttpClient, I was using a HttpHandler and set the Credentials on it like this:

This gave me an 401 error on IOS, so I managed to work around this by setting the authorization header by myself:byte[] authBytes = Encoding.UTF8.GetBytes(_account.Username + ":" + _account.Password); httpRequestMessage.Headers.Add("Authorization", "BASIC " + Convert.ToBase64String(authBytes));

@Guido_Kersten - thanks for the workaround, I've just made the change to my app and it now works. Only change I made to your code was to use Basic instead of BASIC. Not sure all the scenarios but for what I was using it failed if it was all in uppercase.

Hello guys,
I am experiencing the same problem with xamarin.forms and NTLM authentication. the Get methods works perfectly but the Post methods fails to pass the Credentials. it works also on windows phone: both Get and Post Methods. But not on iOS nor Android. Did someone came up with a work around?? Is this bug fixed Yet??
My hold project is on hold because of this bug

Hi guys,
I am also having exactly the same problem with latest Xamarin.iOS 8.10.4.46. Below is my code. The same code on Visual Studio works fine, But in Xamarin its doesn't . Its giving 401 Unauthorised error. Here my issue is bit weird. With same credentials I am calling another GetAsync which is giving correct response. At this point Authentication settings in server for my virtual directory is "Windows Authentication Enabled and Anonymous Authentication Disabled."
When I change the settings to Anonymous Authentication Enabled and Windows Authentication also Enabled(as previous). GetAsync doesn't work and PostAsync works fine on Xamarin.

RE: GETs working but POSTs returning 401 Unauthorised when using NTLM authentication, I had the same issue with Xamarin Android 7.0.2.42. After some experimentation I found the following code works for both GET and POST. Hope it helps someone:

We use Xamarin Forms PCL and when we POST an image to an aspx page using windows authentication and not tls1.2 its works fine but when the android or ios app is set to tls1.2 we only can upload a small image, <100kb. When the image is larger the handshake fails.
Does anyone have an idea whats wrong?