title: AutoFac: Inject NULL values
body: <p>I want to use AutoFac to inject the current principal in the objects that need it. Suppose I have an object (<code>AuthorizationValidator</code>) that is performing security checks. It looks something like this:</p>
  
  <pre><code>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
       ...
    }
  }
  </code></pre>
  
  <p>For my web application the AuthorizationValidator is registered and I use the following registration to inject the principal:</p>
  
  <pre><code>builder.Register&lt;IPrincipal&gt;((c, p) =&gt; HttpContext.Current?.User);
  </code></pre>
  
  <p>Other type of applications use the thread's principal or something similar. All object that require the principal get the proper principal injected.</p>
  
  <p>If a call is made without authorization, then AutoFac raises an exception telling me that it cannot provide the <code>IPrincipal</code> object, because the factory returned <code>null</code>. In this case, an empty principal is fine and shouldn't raise an exception.</p>