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) { using (var forumdb = new DataContexts.ForumDbContext()) { UserProfile profile = new UserProfile { UserId = user.Id }; forumdb.UserProfiles.Add(profile); forumdb.SaveChanges(); } await SignInManager.SignInAsync(user, isPersistent : false, rememberBrowser : false); // For more information on how to enable account confirmation and password reset please visit https://go.microsoft.com/fwlink/?LinkID=320771 // Send an email with this link // string code = await UserManager.GenerateEmailConfirmationTokenAsync(user.Id); // var callbackUrl = Url.Action("ConfirmEmail", "Account", new { userId = user.Id, code = code }, protocol: Request.Url.Scheme); // await UserManager.SendEmailAsync(user.Id, "Confirm your account", "Please confirm your account by clicking <a href=\"" + callbackUrl + "\">here</a>"); return(RedirectToAction("Index", "Home")); } AddErrors(result); } // If we got this far, something failed, redisplay form return(View(model)); }
protected override void Seed(DataContexts.ApplicationDbContext context) { // This method will be called after migrating to the latest version. // You can use the DbSet<T>.AddOrUpdate() helper extension method // to avoid creating duplicate seed data. var store = new RoleStore <IdentityRole>(context); var roleManager = new RoleManager <IdentityRole>(store); var userManager = new UserManager <ApplicationUser>(new UserStore <ApplicationUser>(context)); List <IdentityRole> identityRoles = new List <IdentityRole> { new IdentityRole { Name = RoleTypes.Admin }, new IdentityRole { Name = RoleTypes.Moderator }, new IdentityRole { Name = RoleTypes.User } }; foreach (IdentityRole role in identityRoles) { roleManager.Create(role); } var passwordHasher = new PasswordHasher(); var admin = new ApplicationUser { UserName = "******", PasswordHash = passwordHasher.HashPassword("Password@123"), PhoneNumber = "12345678911", Email = "*****@*****.**", SecurityStamp = Guid.NewGuid().ToString() }; context.Users.AddOrUpdate(x => x.UserName, admin); context.SaveChanges(); userManager.AddToRole(admin.Id, RoleTypes.Admin); var users = new List <ApplicationUser> { new ApplicationUser() { UserName = "******", PasswordHash = passwordHasher.HashPassword("Password@1"), PhoneNumber = "12345678911", Email = "*****@*****.**", SecurityStamp = Guid.NewGuid().ToString() }, new ApplicationUser() { UserName = "******", PasswordHash = passwordHasher.HashPassword("Password@2"), PhoneNumber = "12345678911", Email = "*****@*****.**", SecurityStamp = Guid.NewGuid().ToString() }, new ApplicationUser() { UserName = "******", PasswordHash = passwordHasher.HashPassword("Password@3"), PhoneNumber = "12345678911", Email = "*****@*****.**", SecurityStamp = Guid.NewGuid().ToString() }, new ApplicationUser() { UserName = "******", PasswordHash = passwordHasher.HashPassword("Password@123"), Email = "*****@*****.**", SecurityStamp = Guid.NewGuid().ToString() } }; users.ForEach(s => context.Users.AddOrUpdate(x => x.UserName, s)); using (var forumdb = new DataContexts.ForumDbContext()) { if (!forumdb.UserProfiles.Any(u => u.UserId == admin.Id)) { UserProfile profile = new UserProfile { UserId = admin.Id, UserName = "******" }; forumdb.UserProfiles.AddOrUpdate(profile); } foreach (ApplicationUser user in users) { if (forumdb.UserProfiles.Any(u => u.UserId == user.Id)) { continue; } UserProfile profile = new UserProfile { UserId = user.Id, UserName = user.UserName }; forumdb.UserProfiles.AddOrUpdate(profile); } forumdb.SaveChanges(); } }