创建UserManager的这两种方法有什么区别?
var userManager1 = HttpContext.GetOwinContext().GetUserManager<ApplicationUserManager>();
var userManager2 = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(dbContext));
IdentitySamples
应用程序对HttpContext.GetOwinContext
和UserManager
使用RoleManager
。默认MVC 5应用程序模板中的AccountController
也使用HttpContext.GetOwinContext
,但是当我创建一个使用UserManager
的新控制器时。它使用db
(ApplicationDbContext
)
默认AccountController
public ApplicationUserManager UserManager
{
get {
return _userManager ?? HttpContext.GetOwinContext().GetUserManager<ApplicationUserManager>();
}
private set { _userManager = value; }
}
//can be used as
public ActionResult Index()
{
return View(UserManager.Users.ToList());
}
任何新控制器
private ApplicationDbContext db = new ApplicationDbContext();
//can be used as
public ActionResult Index()
{
return View(db.Users.ToList());
}
在哪种情况下应该使用哪两种方法?