I've made a simple piece of OWIN middleware that will get me a User object and add it to HttpContext.Current.Items
so that's available for all controllers and views for each request.
Here's my code:
public class SetCurrentUserMiddleware : OwinMiddleware
{
public SetCurrentUserMiddleware(OwinMiddleware next) : base(next)
{
}
public override Task Invoke(IOwinContext context)
{
if (context.Request.User.Identity.IsAuthenticated)
{
// Do some work to get a userId... (omitted)
var repo = new UserRepository();
User user = repo.Get(userId);
HttpContext.Current.Items["CurrentUserContext"] = user;
}
return Next.Invoke(context);
}
}
I'm using Ninject in my web application - how can I refactor this middleware so that my UserRepository is injected as a dependency? Is this possible at all?
Bind<IContextManager>().To<ContextManager>();
– Muckrake