Switch to identity Sql

This commit is contained in:
Hao Kung 2014-05-07 17:37:09 -07:00
parent 4f7a0390c9
commit be912b15dc
5 changed files with 51 additions and 26 deletions

View File

@ -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;"
}
}
}

View File

@ -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<HttpContext> contextAccessor) : base(manager, contextAccessor) { }
}
public class ApplicationUser : IdentityUser
public class ApplicationUser : User { }
public class ApplicationDbContext : IdentitySqlContext<ApplicationUser>
{
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"));
}
}
}

View File

@ -32,6 +32,21 @@ namespace MusicStore.Models
}
}
public static async Task InitializeIdentityDatabaseAsync(IServiceProvider serviceProvider)
{
using (var db = serviceProvider.GetService<DbContext>() 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);

View File

@ -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<MusicStoreContext>();
@ -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<DbContext, ApplicationDbContext>();
//Bug: https://github.com/aspnet/Identity/issues/50
services.AddIdentity<ApplicationUser, IdentityRole>(s =>
{
//s.UseDbContext(() => context);
//s.UseUserStore(() => new UserStore(context));
s.AddInMemory();
s.AddEntity();
s.AddUserManager<ApplicationUserManager>();
s.AddRoleManager<ApplicationRoleManager>();
});
services.AddTransient<ApplicationSignInManager>();
// Turn off password defaults since register error display blows up
services.SetupOptions<IdentityOptions>(
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
}
/// <summary>
@ -132,22 +121,22 @@ public class Startup
var configuration = serviceProvider.GetService<IConfiguration>();
var userName = configuration.Get("DefaultAdminUsername");
var password = configuration.Get("DefaultAdminPassword");
const string adminRole = "Administrator";
//const string adminRole = "Administrator";
var userManager = serviceProvider.GetService<ApplicationUserManager>();
var roleManager = serviceProvider.GetService<ApplicationRoleManager>();
if (!await roleManager.RoleExistsAsync(adminRole))
{
await roleManager.CreateAsync(new IdentityRole(adminRole));
}
// Todo: identity sql does not support roles yet
//var roleManager = serviceProvider.GetService<ApplicationRoleManager>();
//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"));
}
}

View File

@ -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"
}