Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net mvc 使用HttpContext创建的UserManager与使用DBContext创建的UserManager有什么区别_Asp.net Mvc_Entity Framework_Asp.net Mvc 5_Asp.net Identity - Fatal编程技术网

Asp.net mvc 使用HttpContext创建的UserManager与使用DBContext创建的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

这两种创建UserManager的方法有什么区别

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());
}