Tag Archives: C#

#ddd8 Part 1

By

As you may have picked up from some of my tweets, today I’ve been out and about at #ddd8, a developer meet-up at Microsoft in Reading.

Organised “by the community, for the community” (and mostly by @plip see note below), it features a series of talks and presentations by members of the community on loads of subjects ranging from @RobAshton’s multi-tenant ASP.net MVC overview, to @JonSkeet’s C# 4.

Visual Studio 2010 Beta

By

Following on from my recent post on Office 2010, I thought I’d get on and try the latest piece of beta software that Microsoft has unleashed upon its users.

Visual Studio 2010 is the latest version of Microsoft’s application development workhorse; with it comes version 4 of the .NET Framework, and a whole raft of improvements that developers have long been calling out for.

Visual Studio 2010 Logo

HTTP Authorisation in C#

By

I’ve been doing some work on a super-secret project recently that involves C# and web APIs. As part of this, most of the API calls require HTTP  Basic authorization. This is fairly easy to do, as in C# using a WebClient object you just do this:


WebClient client = new WebClient();

client.Credentials = new NetworkCredentials("username", "password");

client.DownloadFile("http://api.file.here/get.json", "D:\Stuff\Location.doc");

However, the credentials are only sent when the server challenges for authorisation – if it doesn’t explicitly challenge then the credentials aren’t sent. The way around this is to set the header yourself using this little snippet:


WebClient client = new WebClient();

string authInfo = this.credentials.UserName + ":" + this.credentials.Password;
authInfo = Convert.ToBase64String(Encoding.Default.GetBytes(authInfo));
client.Headers["Authorization"] = "Basic " + authInfo;

client.DownloadFile("http://api.file.here/get.json", "D:\Stuff\Location.doc");