Bug 42408 - WebClient.DownloadString returns 401 Unauthorized when using Basic authentication
Summary: WebClient.DownloadString returns 401 Unauthorized when using Basic authentica...
Status: VERIFIED FIXED
Alias: None
Product: Class Libraries
Classification: Mono
Component: System (show other bugs)
Version: 4.5.X
Hardware: Macintosh Mac OS
: High critical
Target Milestone: 4.6.0 (C8)
Assignee: Marek Safar
URL:
Depends on:
Blocks:
 
Reported: 2016-07-07 19:57 UTC by Manish Sinha
Modified: 2016-08-31 20:24 UTC (History)
7 users (show)

Tags: regression,C8Beta1
Is this bug a regression?: Yes
Last known good build: 4.4.2 (mono-4.4.0-branch-c7sr1/f72fe45 Wed Jul 27 16:20:13 EDT 2016)


Attachments
404 Error on client.DownloadString (4.44 KB, application/zip)
2016-08-18 15:30 UTC, Peter Collins
Details


Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.


Please create a new report on GitHub or Developer Community with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:
Status:
VERIFIED FIXED

Description Manish Sinha 2016-07-07 19:57:50 UTC
This snippet throws a WebException in mono-master

client = new WebClient();
client.Headers[HttpRequestHeader.ContentType] = "application/json";
client.Headers.Add("Authorization", "Basic " + Convert.ToBase64String(
    Encoding.ASCII.GetBytes(String.Format( "{0}:{1}", USER_NAME, USER_PASS))
));
client.DownloadString(new Uri(baseUri + relativeUri));



Error

System.Net.WebException : The remote server returned an error: (401) Unauthorized.
  at System.Net.WebClient.DownloadDataInternal (System.Uri address, System.Net.WebRequest& request) <0x30296d8 + 0x0023f> in <filename unknown>:0 
  at System.Net.WebClient.DownloadString (System.Uri address) <0x3029478 + 0x00053> in <filename unknown>:0 
  at (wrapper remoting-invoke-with-check) System.Net.WebClient:DownloadString (System.Uri)
Comment 2 Manish Sinha 2016-07-07 20:01:58 UTC
Fails using Mono 4.0 ( 4.5.2 (master/b07703e Wed Jul  6 06:32:15 EDT 2016
Comment 3 Kyle White 2016-08-09 16:13:19 UTC
It's not only WebClient.DownloadString, UploadString has the same problem. I presume it's a problem with Authorization all together as Bearer authentication has the same problem.

You can see it fail with Bearer authentication with the following test (fails to deserialize the response because the request was unauthorized and the server redirects it to the github oauth screen):
https://github.com/xamarin/QualityAssurance/blob/master/Automation/XQA/XQA.Core/Clients/XqaClientTests.cs#L40-L45
Comment 4 Kyle White 2016-08-09 16:16:33 UTC
Issue exists on: 
Mono JIT compiler version 4.6.0 (mono-4.6.0-branch/28f1e4e Tue Aug  9 09:45:18 EDT 2016)

Issue does not exist on: 
Mono JIT compiler version 4.4.2 (mono-4.4.0-branch-c7sr1/f72fe45 Wed Jul 27 16:20:13 EDT 2016)
Comment 5 Peter Collins 2016-08-18 15:30:22 UTC
Created attachment 17100 [details]
404 Error on client.DownloadString

I'm now getting a 404 error with somewhat similar WebClient usage. I'm not sure if this  has the same cause or should be filed separately though. This error is a regression in mono 4.6.0, the same snippet executes without issue using mono 4.4.x.
Comment 7 Marek Safar 2016-08-31 10:14:29 UTC
Fixed in master and Mono 4.6
Comment 8 Manish Sinha 2016-08-31 20:24:06 UTC
I have verified this using Mono 4.6.0.182