[英]Injecting the current user using Autofac and ASP.Net MVC
我尝试Autofac和描述的架构此文章。 通过使用Ninject,我曾经能够使用以下内容使用Ninject撤回当前用户:
kernel.Bind<ApplicationUser>().ToMethod(c => (System.Web.HttpContext.Current.GetOwinContext().GetUserManager<ApplicationUserManager>()).FindById(System.Web.HttpContext.Current.User.Identity.GetUserId<int>()));
使用上面的内容,您可以轻松访问控制器和存储库中当前登录的用户,例如:
private readonly ApplicationUser _user;
public ProductsController(ApplicationUser user)
{
_user = user;
}
private readonly ApplicationUser _user;
public SectorRepository(ApplicationUser user)
{
_user = user;
}
我已经尝试过Autofac,但到目前为止仍无法获得相同的结果
如何使用Autofac完成相同或相似的工作?
尝试这个:
protected override void Load(ContainerBuilder builder)
{
builder.Register(
c => (HttpContext.Current.GetOwinContext().GetUserManager<ApplicationUserManager>())
.FindById(HttpContext.Current.User.Identity.GetUserId())).As<ApplicationUser>();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.