From 5ba60857a7e5d2f31ab13a6893647ab48cd58ff8 Mon Sep 17 00:00:00 2001 From: Hao Kung Date: Tue, 22 Apr 2014 14:01:42 -0700 Subject: [PATCH] Identity DI changes --- .../Controllers/AccountController.cs | 4 +- src/MusicStore/Models/IdentityModels.cs | 13 ++++- src/MusicStore/Startup.cs | 50 ++++++++++++------- 3 files changed, 47 insertions(+), 20 deletions(-) diff --git a/src/MusicStore/Controllers/AccountController.cs b/src/MusicStore/Controllers/AccountController.cs index 236674783f..540ff07896 100644 --- a/src/MusicStore/Controllers/AccountController.cs +++ b/src/MusicStore/Controllers/AccountController.cs @@ -14,7 +14,7 @@ namespace MusicStore.Controllers { public UserManager UserManager { - get { return Context.ApplicationServices.GetService>(); } + get { return Context.ApplicationServices.GetService(); } } private SignInManager _signInManager; @@ -23,7 +23,7 @@ namespace MusicStore.Controllers { if (_signInManager == null) { - _signInManager = new SignInManager() + _signInManager = new SignInManager { UserManager = UserManager, Context = Context, diff --git a/src/MusicStore/Models/IdentityModels.cs b/src/MusicStore/Models/IdentityModels.cs index 1ecab55b16..0c984ee1c4 100644 --- a/src/MusicStore/Models/IdentityModels.cs +++ b/src/MusicStore/Models/IdentityModels.cs @@ -1,7 +1,18 @@ -using Microsoft.AspNet.Identity; +using System; +using Microsoft.AspNet.Identity; namespace MusicStore.Models { + public class ApplicationUserManager : UserManager + { + public ApplicationUserManager(IServiceProvider services) : base(services) { } + } + + public class ApplicationRoleManager : RoleManager + { + public ApplicationRoleManager(IServiceProvider services) : base(services) { } + } + public class ApplicationUser : IdentityUser { } diff --git a/src/MusicStore/Startup.cs b/src/MusicStore/Startup.cs index 5438617528..085c33a4e7 100644 --- a/src/MusicStore/Startup.cs +++ b/src/MusicStore/Startup.cs @@ -6,6 +6,7 @@ using Microsoft.AspNet.DependencyInjection; using Microsoft.AspNet.DependencyInjection.Fallback; using Microsoft.AspNet.Diagnostics; using Microsoft.AspNet.Identity; +using Microsoft.AspNet.Identity.Entity; using Microsoft.AspNet.Identity.InMemory; using Microsoft.AspNet.Identity.Security; using Microsoft.AspNet.Logging; @@ -21,11 +22,11 @@ using MusicStore.Logging; using MusicStore.Models; using MusicStore.Web.Models; using System; +using System.Collections.Generic; using System.IO; public class Startup { - public void Configuration(IBuilder app) { app.UseServices(services => @@ -38,8 +39,19 @@ public class Startup services.AddEntityFramework(s => s.AddInMemoryStore()); #endif services.AddTransient(); - services.AddInstance>(new UserManager(new InMemoryUserStore())); - services.AddInstance>(new RoleManager(new InMemoryRoleStore())); + // File an issue trying to use IdentityUser/IdentityRole and open generic UserManager<> + services.AddIdentity(s => + { + // Turn off password defaults since register error display blows up + s.UsePasswordValidator(() => new PasswordValidator()); + + //s.UseDbContext(() => context); + //s.UseUserStore(() => new UserStore(context)); + s.UseUserStore(() => new InMemoryUserStore()); + s.UseUserManager(); + s.UseRoleStore(() => new InMemoryRoleStore()); + s.UseRoleManager(); + }); }); //ErrorPageOptions.ShowAll to be used only at development time. Not recommended for production. @@ -50,7 +62,13 @@ public class Startup app.UseCookieAuthentication(new CookieAuthenticationOptions() { AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie, - LoginPath = new PathString("/Account/Login") + LoginPath = new PathString("/Account/Login"), + Notifications = new CookieAuthenticationNotifications + { + //OnValidateIdentity = SecurityStampValidator.OnValidateIdentity( + // validateInterval: TimeSpan.FromMinutes(30), + // regenerateIdentity: (manager, user) => user.GenerateUserIdentityAsync(manager)) + } }); app.UseMvc(routes => @@ -76,26 +94,24 @@ public class Startup configuration.AddEnvironmentVariables(); //If configuration flows through environment we should pick that first configuration.AddJsonFile(Path.Combine(applicationEnvironment.ApplicationBasePath, "Config.json")); - string _username = configuration.Get("DefaultAdminUsername"); - string _password = configuration.Get("DefaultAdminPassword"); - string _role = "Administrator"; + var userName = configuration.Get("DefaultAdminUsername"); + var password = configuration.Get("DefaultAdminPassword"); + const string adminRole = "Administrator"; - var userManager = serviceProvider.GetService>(); - var roleManager = serviceProvider.GetService>(); + var userManager = serviceProvider.GetService(); + var roleManager = serviceProvider.GetService(); - var role = new IdentityRole(_role); - var result = await roleManager.RoleExistsAsync(_role); - if (result == false) + if (!await roleManager.RoleExistsAsync(adminRole)) { - await roleManager.CreateAsync(role); + await roleManager.CreateAsync(new IdentityRole(adminRole)); } - var user = await userManager.FindByNameAsync(_username); + var user = await userManager.FindByNameAsync(userName); if (user == null) { - user = new ApplicationUser { UserName = _username }; - await userManager.CreateAsync(user, _password); - await userManager.AddToRoleAsync(user, _role); + user = new ApplicationUser { UserName = userName }; + await userManager.CreateAsync(user, password); + await userManager.AddToRoleAsync(user, adminRole); } } } \ No newline at end of file