Asp.net mvc 使用HttpContext创建的UserManager与使用DBContext创建的UserManager有什么区别
这两种创建UserManager的方法有什么区别Asp.net mvc 使用HttpContext创建的UserManager与使用DBContext创建的UserManager有什么区别,asp.net-mvc,entity-framework,asp.net-mvc-5,asp.net-identity,Asp.net Mvc,Entity Framework,Asp.net Mvc 5,Asp.net Identity,这两种创建UserManager的方法有什么区别 var userManager1 = HttpContext.GetOwinContext().GetUserManager<ApplicationUserManager>(); var userManager2 = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(dbContext)); 任何新的控制器 private Appl
var userManager1 = HttpContext.GetOwinContext().GetUserManager<ApplicationUserManager>();
var userManager2 = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(dbContext));
任何新的控制器
private ApplicationDbContext db = new ApplicationDbContext();
//can be used as
public ActionResult Index()
{
return View(db.Users.ToList());
}
在哪种情况下应使用这两种方法中的哪一种?IMO,您不应使用这两种方法中的任何一种。相反,将
UserManager
设置为控制反转容器中的依赖项,然后构造函数在需要的地方注入它。除此之外,UserManager
提供了比普通的DbContext
更多的行为,例如密码散列和电子邮件确认功能。
private ApplicationDbContext db = new ApplicationDbContext();
//can be used as
public ActionResult Index()
{
return View(db.Users.ToList());
}