Using Multiple Auth Handlers In ASP.NET Core
ASP.NET Core makes it extremely easy to configure authentication right out of the box with a choice from a plethora of different built-in authentication handlers. Everything from Single Sign On with Facebook to JWT to simple cookie authentication is available right out of the box. Where I found the ASP.NET Core documentation lacking was when attempting to use multiple authentication handlers at the same time. I was already using the JWT handler in my application, but I wanted to have my custom API key authentication handler run and handle authentication if no Authorization header was supplied as part of the request. Finding the answer to how to accomplish that took longer than it should have, so hopefully this blog post will save others from that same fate.
How to Load Balance SignalR Without Using Sticky Sessions
SignalR poses some small challenges when running in a load balanced environment. When you have multiple servers then some users will have SignalR connections open to one server and then some users will have SignalR connections open to another. The usual solution is to use a SignalR backplane so that all the servers in your cluster can see every SignalR message that was sent, then forward the message to the appropriate users connected to each server. This way users connected to Server B can see SignalR messages sent from Server A. Then you smack a load balancer in front of all the SignalR servers so that you can evenly distribute traffic across all of them, the only problem is that traditionally you have to use sticky sessions on your load balancer in order to make SignalR work.
Designing With Testability in Mind
Although there are some special considerations, designing for testability largely involves following well established software design principles. In this blog post I’ll cover these design principles and how they help with testability and also cover any special considerations that you’ll need to keep in mind.
Useful Software Design Principles
- Single Responsiblity Principle
For those not familiar with the Single Responsiblity Principle (SRP), it was originally proposed by Uncle Bob Martin and states that a class should have a single responsibility or reason to change. The most important thing about this principle is that it promotes the separation of concerns. Consider the example below:
Writing Your First Unit Test
For the rest of the articles in this series, we’ll be using the excellent
xUnit.net unit testing framework to write tests. This is the unit testing
framework with the greatest amount of mindshare at the moment, with Microsoft having recently adopted
it for the various ASP.NET projects that they have open sourced.
Before we begin, we’ll need to cover some simple concepts around how to write your tests. When
writing a unit test, the thing being tested is called the System Under Test (or SUT for short).
The SUT can be just be about anything but is typically a method in a particular class.
Since a unit test should only test a single thing then it follows that each of your tests should
only have a single SUT.
Introduction to Unit Testing
Let’s be honest, manually testing software sucks. When developing a new feature, you’ll often have to manually test your changes multiple times to ensure everything is working. If you are a web developer, this usually means jumping through hoops in order to test your code through the UI. What you really want is a way to automate these tests and run them whenever you make a change. Even better would be a way to run these tests everytime someone else makes a change to the software as well. This is where unit testing and automated testing in general comes in.