Bug 42408 - WebClient.DownloadString returns 401 Unauthorized when using Basic authentication
Summary: WebClient.DownloadString returns 401 Unauthorized when using Basic authentica...
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
Depends on:
Reported: 2016-07-07 19:57 UTC by Manish Sinha
Modified: 2016-08-31 20:24 UTC (History)
7 users (show)

See Also:
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)

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

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));


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):
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

Notice (2018-05-21): bugzilla.xamarin.com will be switching to read-only mode on Thursday, 2018-05-25 22:00 UTC.

Please join us on Visual Studio Developer Community and GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs and copy them to the new locations as needed for follow-up. The See Also field on each Bugzilla bug will be updated with a link to its new location when applicable.

After Bugzilla is read-only, if you have new information to add for a bug that does not yet have a matching issue on Developer Community or GitHub, you can create a follow-up issue in the new location. Copy and paste the title and description from this bug, and then add your new details. You can get a pre-formatted version of the title and description here:

In special cases you might also want the comments:

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.

Note You need to log in before you can comment on or make changes to this bug.