开发者

How can I access a REST API through Silverlight (With basic auth)?

开发者 https://www.devze.com 2023-02-27 01:42 出处:网络
I\'m trying to access a run-of-the-mill REST API through a silverlight application, but can\'t seem to get basic authentication to work (using silverlight 4.0).

I'm trying to access a run-of-the-mill REST API through a silverlight application, but can't seem to get basic authentication to work (using silverlight 4.0).

I'm using a 开发者_运维百科.NET WebClient, and setting the webclient.Credentials with a valid username/password (for the API).

I'm running into two main errors:

System.NotSupportedException: BrowserHttpWebRequest does not support custom credentials. (this only occurs when I set the webclient.UseDefaulCredentials to false)

and

System.NotImplementedException: This property is not implemented by this class. at System.Net.WebRequest.set_Credentials(ICredentials value) (occurs when webclient.UseDefaultCredentials isn't set at all)

what am I missing here? Is basic auth still not supported in Silverlight 4.0?


There is an article about it here http://mark.mymonster.nl/2009/12/02/silverlight-4-credentials-weve-got-it/ and it seems the magic line is:

WebRequest.RegisterPrefix("http://", System.Net.Browser.WebRequestCreator.ClientHttp);

In my case it seems to send the credentials, but I still end up with a 401 error but I just grabbed one of my web servers to try and it may not be configured correctly, but this seems like it should work.


I would also suggest trying the open source library Hammock for REST It provides a very useful wrapper around a lot of modern web calls like REST and oAuth.

0

精彩评论

暂无评论...
验证码 换一张
取 消