WCF HTTP Basic Authentication in Code

So I wanted to build a WCF service with a very simple authentication mechanism, and I thought that it would be good to start with my own implementation of http basic authentication - and then I could go from there, adding my own extras to the protocol if I wanted. But I thought that basic authentication over https would at least be a start.

And even though the username and password are sent in clear text, doing that over an https connection should be safe since it's encrypted before going over the wire.

Finally, I wanted to do the whole lot in code, so that I can deploy it to some hosted space without fiddling around in IIS. But also because I like code :-)

The good thing is that basic authentication is supported by most browsers, so I could expose the WCF service as web methods and test it very easily. I have assumed that since it works with all the major browsers I have implemented the protocol correctly.

The end result was a custom attribute which can be added to a WCF service and does all the work for you. The only other thing that needs to be done is modify the method which decides if a user is valid or not.

Here is the source code (click the image to download):

I'll post an example of how to use it (in Visual Studio 2012) in another blog.

Easy Windows Service Development

I've just discovered Topshelf for creating Windows Services in .Net. It makes building Windows Services a breeze, and you can also run them in the Visual Studio debugger easily too. Just look at the simple code example here ... and you only need to create a console app to make this example work.
Adding Topshelf to your project is easy thanks to NuGet. Your service code just needs to be a plain object and doesn't need to inherit from any special base class. It makes the whole thing extremely simple.

When you've compiled your console app, you can run it in the debugger to check it, and when you want to install it as a proper service, you just pass "install" as a command line argument when you run the exe. To remove the service you re-run the console app and pass "uninstall" on the command line.