Disposing of UserManager and UserStore outside of AccountController?
Is that good enough? Or do I need to dispose UserStore
? If I have any suggestions, we'd love to. I am new to ASP.NET Identity.
using (var applicationDbContext = new ApplicationDbContext())
{
using (var userManager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(applicationDbContext)))
{
}
}
It would be better I think:
using (var applicationDbContext = new ApplicationDbContext())
{
using (var userStore = new UserStore<ApplicationUser>(applicationDbContext))
{
using (var userManager = new UserManager<ApplicationUser>(userStore))
{
}
}
}
EDIT: I'm glad I asked this question, although it may have already answered my original question. Thanks Glenn Ferry for checking out ASP.NET Dependency Injection.
+3
source to share
1 answer
These are some code snippets from the new ASP.NET MVC (.NET 4.6) built with VS 2015 RC. First the class 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.
then this is how you access it in the 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!
+2
source to share