0

I'm trying out Autofac and the architecture described in this article. With Ninject I used to be able to use the following to retireve the current user using Ninject:

kernel.Bind<ApplicationUser>().ToMethod(c => (System.Web.HttpContext.Current.GetOwinContext().GetUserManager<ApplicationUserManager>()).FindById(System.Web.HttpContext.Current.User.Identity.GetUserId<int>()));

Using the above, one could then easily get access to the currently logged in user in both your controllers and repositories e.g:

private readonly ApplicationUser _user;
public ProductsController(ApplicationUser user)
{
    _user = user;
}

private readonly ApplicationUser _user;
public SectorRepository(ApplicationUser user)
{
    _user = user;
}

I've experimented with Autofac and so far could not achieve the same results

How would I be able to accomplish the same or similar using Autofac?

1 Answer 1

2

Try this:

protected override void Load(ContainerBuilder builder)
{
       builder.Register(
                c => (HttpContext.Current.GetOwinContext().GetUserManager<ApplicationUserManager>())
                    .FindById(HttpContext.Current.User.Identity.GetUserId())).As<ApplicationUser>();
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.