I want to use AutoFac to inject the current principal in the objects that need it. Suppose I have an object (AuthorizationValidator
) that is performing security checks. It looks something like this:
public AuthorizationValidator : IAuthorizationValidator
{
public AuthorizationValidator(IDataAccess dataAccess, IPrincipal principal)
{
// Save injected objects
...
}
public bool CheckPermission(Guid objectId, Action action)
{
// Check if we are authorized at all
if (this.principal == null)
return false;
// Check the permission in the database
...
}
}
For my web application the AuthorizationValidator is registered and I use the following registration to inject the principal:
builder.Register<IPrincipal>((c, p) => HttpContext.Current?.User);
Other type of applications use the thread's principal or something similar. All object that require the principal get the proper principal injected.
If a call is made without authorization, then AutoFac raises an exception telling me that it cannot provide the IPrincipal
object, because the factory returned null
. In this case, an empty principal is fine and shouldn't raise an exception.
In the Autofac documentation they recommend to use the Null Object pattern for such scenarios. You could create a
NullPrincipal
class that inherits from theIPrincipal
interface with a private constructor that only exposes areadonly
static
field which provides the default instance. Then you can return this instance instead of null:Of course you would have to update all places in your code where you are checking if the principal is null and check if it is equal to the
NullPrincipal.Default
instead.To solve this problem, I have created the
IPrincipalFactory
interface that can obtain the current principal without going through AutoFac:For the ASP.NET application I would register the following object as IPrincipalFactory:
Although this works, I am not completely happy with this solution.