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 the IPrincipal
interface with a private constructor that only exposes a readonly
static
field which provides the default instance. Then you can return this instance instead of null:
builder.Register<IPrincipal>((c, p) => HttpContext.Current?.User ?? NullPrincipal.Default);
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With