Basic Authentication for Silverlight Browser

I cannot connect to this url using WebRequestCreator.BrowserHttp, but I can connect using WebRequestCreator.ClientHttp. Here is a sample code that I use,

var httpClient = new HttpClient();
WebRequest.RegisterPrefix("http://", WebRequestCreator.BrowserHttp);
var byteArray = Encoding.UTF8.GetBytes("username:password");
httpClient.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Basic", Convert.ToBase64String(byteArray));
var response = await httpClient.GetAsync(url, HttpCompletionOption.ResponseHeadersRead);

I am trying to avoid using the Windows Security dialog box and I cannot use WebRequestCreator.BrowserHttpfor my project.

Edit:

When using, WebRequestCreator.BrowserHttpI get

System.ArgumentException: value is not in the expected range

and nothing in the violinist. If I use WebRequestCreator.ClientHttp, I get

Authorization: Basic 

in fiddler

+4
source share
1 answer

Basic Bearer (JWT) silverlight.

/* 
* NOTE:
* It turns out that Silverlight provides HTTP handling in both the Browser and Client stack. 
* By default Silverlight uses the Browser for HTTP handling. 
* The only problem with this is that Browser HTTP Handling only supports GET and POST request methods.
*/
WebRequest.RegisterPrefix("http://", WebRequestCreator.ClientHttp);
WebRequest.RegisterPrefix("https://", WebRequestCreator.ClientHttp);
var httpClient = new HttpClient();
var byteArray = Encoding.UTF8.GetBytes("username:password");
// Default headers will be sent with every request sent from this HttpClient instance.
httpClient.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Basic", Convert.ToBase64String(byteArray));
var response = await httpClient.GetAsync(url, HttpCompletionOption.ResponseHeadersRead);
0

All Articles