Silverlight浏览器的基本身份验证

时间:2015-01-21 20:02:13

标签: c# security silverlight url

我无法使用WebRequestCreator.BrowserHttp连接到此网址,但我可以使用WebRequestCreator.ClientHttp进行连接。这是我正在使用的代码的示例,

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

我正在尝试避免使用“Windows安全”对话框,我无法将WebRequestCreator.BrowserHttp用于我的项目。

修改

使用WebRequestCreator.BrowserHttp时,我

  

System.ArgumentException:值不在预期范围内   范围

招潮中没有任何东西。如果我使用WebRequestCreator.ClientHttp,我会

Authorization: Basic 

在Fiddler

1 个答案:

答案 0 :(得分:0)

我能够在当前的silverlight项目上进行基本和承载(JWT)身份验证。

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