Search code examples
silverlightwebclientbasic-authentication

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


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?


Solution

  • 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.