A few months ago,
I investigated Silverlight 3 Beta on support for Credentials. There are two classes that give a little bit of notion that they are supporting Credentials. Both
WebClient and
WebRequest have a property
Credentials of type
ICredentials. Let’s try them one by one.
WebRequest credentials support? First we need to have some code to request the content behind an url and assign a set of credentials to the request.
WebRequest request = HttpWebRequest.Create("http://mark.mymonster.nl");
request.Credentials = new NetworkCredential("username", "password"); Very straightforward code which you can write for the full CLR as well. This code doesn’t even contain the execution of the request.
Yes it fails. If you try to set the de Credentials you will get a
NotImplementedException. Sad, so we have not support for credentials in the WebRequest.
WebRequest credentials support outcome: Negative WebClient credentials support? Next class with a Credentials property, WebClient. First let’s write some code.
var client = new WebClient
{
Credentials =
new NetworkCredential("username", "password")
};
client.DownloadStringCompleted += client_DownloadStringCompleted;
client.DownloadStringAsync(new Uri("http://mark.mymonster.nl"));
Of course if anything goes wrong we can see it in the DownloadStringCompleted. From QuickWatch, we can see what happened.
Hmm, I could have guessed that WebClient internally makes use of WebRequest. ...