Is this good enough? Or do i have to dispose UserStore
as well? If i do have to any suggestions would be welcome. I'm new to ASP.NET Identity.
using (var applicationDbContext = new ApplicationDbContext())
{
using (var userManager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(applicationDbContext)))
{
}
}
This would be better i guess:
using (var applicationDbContext = new ApplicationDbContext())
{
using (var userStore = new UserStore<ApplicationUser>(applicationDbContext))
{
using (var userManager = new UserManager<ApplicationUser>(userStore))
{
}
}
}
EDIT: I'm glad that i asked this question, although i may have already answered my initial question. Thanks Glenn Ferrie, will check out the ASP.NET dependency injection.
This is a few code snippets from a new ASP.NET MVC (.NET 4.6) created with VS 2015 RC. First the Startup
class:
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.
then here is how you access it in a Controller class:
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
Happy Coding!
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.