1.8.4.Seeding Users and Roles
1.Register a guest account in the home page
2.Modify the AccountController
public async Task<ActionResult> Register(RegisterViewModel model) { if (ModelState.IsValid) { var user = new ApplicationUser { UserName = model.Email, Email = model.Email }; var result = await UserManager.CreateAsync(user, model.Password); if (result.Succeeded) { //Temp code var roleStore = new RoleStore<IdentityRole>(new ApplicationDbContext()); var roleManager = new RoleManager<IdentityRole>(roleStore); await roleManager.CreateAsync(new IdentityRole("CanManageMovie")); await UserManager.AddToRoleAsync(user.Id, "CanManageMovie");
3.Register a admin account in the home page
4.Remove the code on step 2 and rebuild
5.On nuget package condole, type:
add-migration "seed users"
6.Go to server explorer, copy the script from AspNetUsers, AspNetRoles, AspNetUserRoles and paste them on the migration file
public override void Up() { Sql(@" INSERT INTO [dbo].[AspNetUsers] ([Id], [Email], [EmailConfirmed], [PasswordHash], [SecurityStamp], [PhoneNumber], [PhoneNumberConfirmed], [TwoFactorEnabled], [LockoutEndDateUtc], [LockoutEnabled], [AccessFailedCount], [UserName]) VALUES (N'0480be5b-da0a-4534-9290-cb7783d9279b', N'realAdmin@vidly.com', 0, N'AKQG+TyYJuDRNSDZ0AHGi/uza4rVCTMTVTB1MhPEe0bny9Fvr03tce6MoOjD/9rOyw==', N'b7b4bac9-6d79-40dd-9b3d-4258dc6d6c3c', NULL, 0, 0, NULL, 1, 0, N'realAdmin@vidly.com') INSERT INTO [dbo].[AspNetUsers] ([Id], [Email], [EmailConfirmed], [PasswordHash], [SecurityStamp], [PhoneNumber], [PhoneNumberConfirmed], [TwoFactorEnabled], [LockoutEndDateUtc], [LockoutEnabled], [AccessFailedCount], [UserName]) VALUES (N'7a89fdd8-6333-45ac-806f-29de93b53c96', N'admin@vidly.com', 0, N'AFGsGmUalL+ssBOrIRxBS/h2tB/cw9f3aSWDIvOITxPpZcnJFYRxvvzuUGptQByv+Q==', N'7afb5815-7d1f-452a-bc7d-b1aebe3b66d9', NULL, 0, 0, NULL, 1, 0, N'admin@vidly.com') INSERT INTO [dbo].[AspNetRoles] ([Id], [Name]) VALUES (N'e8b29ebf-63be-484a-9348-2e48225d6d9d', N'CanManageMovie') INSERT INTO [dbo].[AspNetUserRoles] ([UserId], [RoleId]) VALUES (N'0480be5b-da0a-4534-9290-cb7783d9279b', N'e8b29ebf-63be-484a-9348-2e48225d6d9d') "); }
7.On nuget package condole, type:
update database
Last updated
Was this helpful?