这够好吗?或者我是否也必须处置UserStore
?如果我有任何建议,欢迎。我是ASP.NET身份的新手。
using (var applicationDbContext = new ApplicationDbContext())
{
using (var userManager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(applicationDbContext)))
{
}
}
我想这会更好:
using (var applicationDbContext = new ApplicationDbContext())
{
using (var userStore = new UserStore<ApplicationUser>(applicationDbContext))
{
using (var userManager = new UserManager<ApplicationUser>(userStore))
{
}
}
}
编辑:我很高兴我问过这个问题,虽然我可能已经回答了我的初步问题。感谢Glenn Ferrie,将检查ASP.NET依赖注入。
答案 0 :(得分:2)
这是使用VS 2015 RC创建的新ASP.NET MVC(.NET 4.6)的一些代码片段。首先是Startup
类:
public partial class Startup
{
// For more information on configuring authentication, please visit http://go.microsoft.com/fwlink/?LinkId=301864
public void ConfigureAuth(IAppBuilder app)
{
// Configure the db context, user manager and signin manager to use a single instance per request
app.CreatePerOwinContext(ApplicationDbContext.Create);
app.CreatePerOwinContext<ApplicationUserManager>(ApplicationUserManager.Create);
app.CreatePerOwinContext<ApplicationSignInManager>(ApplicationSignInManager.Create);
// rest of implementation ommitted for brevity.
然后,您将在Controller类中访问它:
public class AccountController : Controller
{
private ApplicationSignInManager _signInManager;
private ApplicationUserManager _userManager;
public AccountController()
{
}
// NOTE: ASP.NET will use this contructor and inject the instances
// of SignInManager and UserManager from the OWIN container
public AccountController(ApplicationUserManager userManager, ApplicationSignInManager signInManager )
{
UserManager = userManager;
SignInManager = signInManager;
}
// there are implementations for the public properties
// 'UserManager' and 'SignInManager' in the boiler plate code
// not shown here
快乐的编码!