title: Simple Injector get current principal in WebAPI using OWIN
body: <p>Is there anyway to access the current principal and  before the request gets to the controller using Simple Injector? I am using OWIN and Asp.net identity.</p>
  
  <p>I have a DbContext that I inject into my controllers, but this context will get it's connection string based off the authenticated user. This is what I have so far,</p>
  
  <pre><code>container.RegisterWebApiRequest&lt;TenantDbContext&gt;();
  container.RegisterWebApiRequest&lt;ITenantConnectionStringProvider&gt;(() =&gt; new TenantConnectionStringProvider(container));
  </code></pre>
  
  <p>Then in my TenantConnectionStringProvider I have this,</p>
  
  <pre><code>var request = container.GetCurrentHttpRequestMessage();
  var principal = request.GetRequestContext().Principal as ClaimsPrincipal;
  </code></pre>
  
  <p>But the principal has no claims. I realized the claims are only available after the controller has been created. Does this mean it's just not possible because this step comes before the controller is created?</p>
  
  <p>Edit: 
  This is basically what the rest of the code does:</p>
  
  <p>WebApi Controller</p>
  
  <pre><code>    public CampaignsController(TenantDbContext context, ILog log)
      {
          this.campaignService = campaignService;
          this.log = log;
      }
  </code></pre>
  
  <p>Tenant context(just inherits from DbContext from EF):</p>
  
  <pre><code>    public TenantDbContext(ITenantConnectionStringProvider provider)
          : base(provider.GetConnectionString())
      {
      }
  </code></pre>
  
  <p>After messing around a bit, I was able to do this, but it feels very hacky..
  I added an OWIN middleware that happens after authentication. I'm not sure why but I have all the authenticated users information here, but when it goes to the TenantConnectionStringProvider, none of this info is available on the HttpRequestMessage.</p>
  
  <pre><code>        app.Use(async (context, next) =&gt;
          {
              using (container.BeginExecutionContextScope())
              {
                  CallContext.LogicalSetData("Claims", context.Authentication.User.Claims);
                  var request = (OwinRequest)context.Request;
                  await next();
              }
          });
  </code></pre>
  
  <p>Then in my TenantConnectionStringProvider I just did this,</p>
  
  <pre><code>    public string GetConnectionString()
      {
          var context = (IEnumerable&lt;Claim&gt;)CallContext.LogicalGetData("Claims");
          return "test";//get claim from context to get the connection string
      }
  </code></pre>