Bug 42408

Summary: WebClient.DownloadString returns 401 Unauthorized when using Basic authentication
Product: [Mono] Class Libraries Reporter: Manish Sinha <masinha>
Component: SystemAssignee: Marek Safar <masafa>
Status: VERIFIED FIXED    
Severity: critical CC: craig.dunn, kyle.white, lexas, masafa, mono-bugs+mono, peter.collins, rustam.zaitov
Priority: High    
Version: 4.5.X   
Target Milestone: 4.6.0 (C8)   
Hardware: Macintosh   
OS: Mac OS   
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

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