How-to: Hawk authentication scheme with SignalR

On a recent machine-to-machine communication scenario I needed to enable secure push messages. The server side of the project was plain ASP.NET Web API 2 (on OWIN – I just love it) so dropping in SignalR and a few hubs was done in minutes. The tricky part was that I was using the Hawk authentication scheme to secure the access (and I was very happy on how it worked out so far). So no reason to change anything about the global setup if it wasn’t for the .NET SignalR client library not supporting Hawk out-of-the-box.

But luckily SignalR sources can be crawled through easily over at github (https://github.com/SignalR/SignalR). And even more luckily I found that it would be no more that a few lines to wire it up with Thinktecture’s Hawk implementation (Thinktecture.IdentityModel.Hawk on Nuget).

So there you go, my latest, greatest Nuget packages is: Pysco68.SignalR.HawkHandler.

To install it:

Using that package isn’t much more complicated than:

Under the hood the whole thing boils down to implementing a IHttpClient that uses Thinktecture’s HawkValidationHandler. If you want to take a look at the inner parts: Pysco68.SignalR.HawkHandler at Github. Have fun reading!

On the server-side the complete setup is really no-fuss. Just wire up authentication and SignalR as you would do in any other scenario, “it just works”™.
You will need to install Thinktecture.IdentityModel.Hawk first, then in your application setup it all comes down to (with a bunch of using statements added…):

Btw. the “it just works” part really – REALLY – is the one thing to love about OWIN in my opinion. Anyway, if you’ve done everything right you should have working Hawk authentication scheme with SignalR right now!

Feel free to ask any question below or on Github.

Hey! Sharing's great for your karma ;)
Tweet about this on TwitterShare on Facebook0Share on Google+0Pin on Pinterest0Email this to someone

About Yannic

Yannic is a daytime freelance software developer, inventor and full-time geek. There's not much technical stuff he wouldn't like to crack open to see how they work. As a perfectionist most of the things he's getting into are completed in the most fashionable manner.