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.
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,
container.RegisterWebApiRequest<TenantDbContext>(); container.RegisterWebApiRequest<ITenantConnectionStringProvider>(() => new TenantConnectionStringProvider(container)); Then in my TenantConnectionStringProvider I have this,
var request = container.GetCurrentHttpRequestMessage(); var principal = request.GetRequestContext().Principal as ClaimsPrincipal; 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?
Edit: This is basically what the rest of the code does:
WebApi Controller
public CampaignsController(TenantDbContext context, ILog log) { this.campaignService = campaignService; this.log = log; } Tenant context(just inherits from DbContext from EF):
public TenantDbContext(ITenantConnectionStringProvider provider) : base(provider.GetConnectionString()) { } 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.
app.Use(async (context, next) => { using (container.BeginExecutionContextScope()) { CallContext.LogicalSetData("Claims", context.Authentication.User.Claims); var request = (OwinRequest)context.Request; await next(); } }); Then in my TenantConnectionStringProvider I just did this,
public string GetConnectionString() { var context = (IEnumerable<Claim>)CallContext.LogicalGetData("Claims"); return "test";//get claim from context to get the connection string }