From be912b15dc62f0050c76cee0ff728f65a8a8224e Mon Sep 17 00:00:00 2001 From: Hao Kung Date: Wed, 7 May 2014 17:37:09 -0700 Subject: [PATCH] Switch to identity Sql --- src/MusicStore/LocalConfig.json | 5 +++- src/MusicStore/Models/IdentityModels.cs | 21 +++++++++++++-- src/MusicStore/Models/SampleData.cs | 15 +++++++++++ src/MusicStore/Startup.cs | 35 +++++++++---------------- src/MusicStore/project.json | 1 + 5 files changed, 51 insertions(+), 26 deletions(-) diff --git a/src/MusicStore/LocalConfig.json b/src/MusicStore/LocalConfig.json index 305952680d..95d4b63d82 100644 --- a/src/MusicStore/LocalConfig.json +++ b/src/MusicStore/LocalConfig.json @@ -1,9 +1,12 @@ { "DefaultAdminUsername": "Administrator", - "DefaultAdminPassword": "YouShouldChangeThisPassword", + "DefaultAdminPassword": "YouShouldChangeThisPassword1!", "Data": { "DefaultConnection": { "Connectionstring": "Server=(localdb)\\v11.0;Database=MusicStore;Trusted_Connection=True;" + }, + "IdentityConnection": { + "Connectionstring": "Server=(localdb)\\v11.0;Database=MusicStoreIdentity;Trusted_Connection=True;" } } } \ No newline at end of file diff --git a/src/MusicStore/Models/IdentityModels.cs b/src/MusicStore/Models/IdentityModels.cs index 699582e9ef..c7ee5b4103 100644 --- a/src/MusicStore/Models/IdentityModels.cs +++ b/src/MusicStore/Models/IdentityModels.cs @@ -1,8 +1,11 @@ using System; using Microsoft.AspNet.Http; -using Microsoft.Framework.DependencyInjection; using Microsoft.AspNet.Identity; +using Microsoft.AspNet.Identity.Entity; using Microsoft.AspNet.Identity.Security; +using Microsoft.Data.Entity; +using Microsoft.Framework.ConfigurationModel; +using Microsoft.Framework.DependencyInjection; namespace MusicStore.Models { @@ -21,7 +24,21 @@ namespace MusicStore.Models public ApplicationSignInManager(ApplicationUserManager manager, IContextAccessor contextAccessor) : base(manager, contextAccessor) { } } - public class ApplicationUser : IdentityUser + public class ApplicationUser : User { } + + public class ApplicationDbContext : IdentitySqlContext { + private readonly IConfiguration _configuration; + + public ApplicationDbContext(IServiceProvider serviceProvider, IConfiguration configuration) + : base(serviceProvider) + { + _configuration = configuration; + } + + protected override void OnConfiguring(DbContextOptions builder) + { + builder.UseSqlServer(_configuration.Get("Data:IdentityConnection:ConnectionString")); + } } } \ No newline at end of file diff --git a/src/MusicStore/Models/SampleData.cs b/src/MusicStore/Models/SampleData.cs index 5e01ed2bab..7cce58fd98 100644 --- a/src/MusicStore/Models/SampleData.cs +++ b/src/MusicStore/Models/SampleData.cs @@ -32,6 +32,21 @@ namespace MusicStore.Models } } + public static async Task InitializeIdentityDatabaseAsync(IServiceProvider serviceProvider) + { + using (var db = serviceProvider.GetService() as ApplicationDbContext) + { + var sqlServerDataStore = db.Configuration.DataStore as SqlServerDataStore; + if (sqlServerDataStore != null) + { + if (!await db.Database.ExistsAsync()) + { + await db.Database.CreateAsync(); + } + } + } + } + private static async Task InsertTestData(IServiceProvider serviceProvider) { var albums = GetAlbums(imgUrl, Genres, Artists); diff --git a/src/MusicStore/Startup.cs b/src/MusicStore/Startup.cs index b12f51635d..46913a8776 100644 --- a/src/MusicStore/Startup.cs +++ b/src/MusicStore/Startup.cs @@ -50,11 +50,8 @@ public class Startup /*Add all EF related services to IoC. Using an InMemoryStore in K until SQL server is available.*/ -#if NET45 services.AddEntityFramework(s => s.AddSqlServer()); -#else services.AddEntityFramework(s => s.AddInMemoryStore()); -#endif services.AddTransient(); @@ -63,27 +60,18 @@ public class Startup * Using an InMemory store to store membership data until SQL server is available. * Users created will be lost on application shutdown. */ + services.AddTransient(); //Bug: https://github.com/aspnet/Identity/issues/50 services.AddIdentity(s => { //s.UseDbContext(() => context); //s.UseUserStore(() => new UserStore(context)); - s.AddInMemory(); + s.AddEntity(); s.AddUserManager(); s.AddRoleManager(); }); services.AddTransient(); - // Turn off password defaults since register error display blows up - services.SetupOptions( - options => - { - options.Password.RequireDigit = false; - options.Password.RequireLowercase = false; - options.Password.RequireNonLetterOrDigit = false; - options.Password.RequireUppercase = false; - options.Password.RequiredLength = 0; - }); }); @@ -117,9 +105,10 @@ public class Startup //Populates the MusicStore sample data SampleData.InitializeMusicStoreDatabaseAsync(app.ApplicationServices).Wait(); + SampleData.InitializeIdentityDatabaseAsync(app.ApplicationServices).Wait(); //Creates a Store manager user who can manage the store. - CreateAdminUser(app.ApplicationServices).Wait(); + //CreateAdminUser(app.ApplicationServices).Wait(); // todo: sql identity doesn't support roles yet } /// @@ -132,22 +121,22 @@ public class Startup var configuration = serviceProvider.GetService(); var userName = configuration.Get("DefaultAdminUsername"); var password = configuration.Get("DefaultAdminPassword"); - const string adminRole = "Administrator"; + //const string adminRole = "Administrator"; var userManager = serviceProvider.GetService(); - var roleManager = serviceProvider.GetService(); - - if (!await roleManager.RoleExistsAsync(adminRole)) - { - await roleManager.CreateAsync(new IdentityRole(adminRole)); - } + // Todo: identity sql does not support roles yet + //var roleManager = serviceProvider.GetService(); + //if (!await roleManager.RoleExistsAsync(adminRole)) + //{ + // await roleManager.CreateAsync(new IdentityRole(adminRole)); + //} var user = await userManager.FindByNameAsync(userName); if (user == null) { user = new ApplicationUser { UserName = userName }; await userManager.CreateAsync(user, password); - await userManager.AddToRoleAsync(user, adminRole); + //await userManager.AddToRoleAsync(user, adminRole); await userManager.AddClaimAsync(user, new Claim("ManageStore", "Allowed")); } } diff --git a/src/MusicStore/project.json b/src/MusicStore/project.json index 4c7e549d37..6a8f3454f5 100644 --- a/src/MusicStore/project.json +++ b/src/MusicStore/project.json @@ -64,6 +64,7 @@ "System.Diagnostics.Tools": "4.0.0.0", "Microsoft.DataAnnotations": "0.1-alpha-*", "System.Reflection": "4.0.10.0", + "System.Reflection.Compatibility": "4.0.0.0", "System.Reflection.Extensions": "4.0.0.0", "System.IO": "4.0.0.0" }