diff --git a/src/MusicStore/Controllers/AccountController.cs b/src/MusicStore/Controllers/AccountController.cs index f21e14c984..236674783f 100644 --- a/src/MusicStore/Controllers/AccountController.cs +++ b/src/MusicStore/Controllers/AccountController.cs @@ -1,4 +1,5 @@ -using Microsoft.AspNet.Identity; +using Microsoft.AspNet.DependencyInjection; +using Microsoft.AspNet.Identity; using Microsoft.AspNet.Identity.Security; using Microsoft.AspNet.Mvc; using Microsoft.AspNet.Mvc.ModelBinding; @@ -11,20 +12,11 @@ namespace MusicStore.Controllers //[Authorize] public class AccountController : Controller { - public AccountController() - //Bug: Using an in memory store - //: this(new UserManager(new UserStore(new ApplicationDbContext()))) - : this(new UserManager(Startup.UserStore)) + public UserManager UserManager { + get { return Context.ApplicationServices.GetService>(); } } - public AccountController(UserManager userManager) - { - UserManager = userManager; - } - - public UserManager UserManager { get; set; } - private SignInManager _signInManager; public SignInManager SignInManager { get diff --git a/src/MusicStore/Controllers/CheckoutController.cs b/src/MusicStore/Controllers/CheckoutController.cs index 0ab123cacb..600a48ede8 100644 --- a/src/MusicStore/Controllers/CheckoutController.cs +++ b/src/MusicStore/Controllers/CheckoutController.cs @@ -1,8 +1,8 @@ -using System.Security.Principal; -using Microsoft.AspNet.Mvc; +using Microsoft.AspNet.Mvc; using MusicStore.Models; using System; using System.Linq; +using System.Security.Principal; using System.Threading.Tasks; namespace MusicStore.Controllers diff --git a/src/MusicStore/Startup.cs b/src/MusicStore/Startup.cs index 78cc57ee85..1c385c26db 100644 --- a/src/MusicStore/Startup.cs +++ b/src/MusicStore/Startup.cs @@ -22,22 +22,18 @@ using System.IO; public class Startup { - /// - /// TODO: Temporary ugly work around (making this static) to enable creating a static InMemory UserManager. Will go away shortly. - /// - public static InMemoryUserStore UserStore = new InMemoryUserStore(); - public static InMemoryRoleStore RoleStore = new InMemoryRoleStore(); private static void ConfigureServices(ServiceCollection services) { services.AddInstance(new NullLoggerFactory()); services.AddMvc(); + + services.AddInstance>(new UserManager(new InMemoryUserStore())); + services.AddInstance>(new RoleManager(new InMemoryRoleStore())); } public void Configuration(IBuilder app) { - CreateAdminUser(app.ServiceProvider); - app.UseContainer(ConfigureServices); //ErrorPageOptions.ShowAll to be used only at development time. Not recommended for production. @@ -67,6 +63,7 @@ public class Startup app.UseRouter(routes); SampleData.InitializeMusicStoreDatabaseAsync().Wait(); + CreateAdminUser(app.ServiceProvider); } private async void CreateAdminUser(IServiceProvider serviceProvider) @@ -81,8 +78,8 @@ public class Startup string _password = configuration.Get("DefaultAdminPassword"); string _role = "Administrator"; - var userManager = new UserManager(UserStore); - var roleManager = new RoleManager(RoleStore); + var userManager = serviceProvider.GetService>(); + var roleManager = serviceProvider.GetService>(); var role = new IdentityRole(_role); var result = await roleManager.RoleExistsAsync(_role);