protected override void Seed(VideoGameMVCLayer.Models.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. E.g. // // context.People.AddOrUpdate( // p => p.FullName, // new Person { FullName = "Andrew Peters" }, // new Person { FullName = "Brice Lambson" }, // new Person { FullName = "Rowan Miller" } // ); // var userStore = new UserStore <ApplicationUser>(context); var userManager = new UserManager <ApplicationUser>(userStore); if (!context.Users.Any(u => u.UserName == "*****@*****.**")) { var user = new ApplicationUser() { UserName = "******", Email = "*****@*****.**", //FirstName = "System", //LastName = "Admin" }; /*Note that I would use a more secure password here, but I need to be able to remember this * and I'm unwilling to use any of my normal passwords.*/ IdentityResult result = userManager.Create(user, "password"); if (result.Succeeded) { userManager.AddClaim(user.Id, new Claim(ClaimTypes.GivenName, "Aaron")); userManager.AddClaim(user.Id, new Claim(ClaimTypes.Surname, "Usher")); context.Roles.AddOrUpdate(r => r.Name, new IdentityRole { Name = "Administrator" }); context.SaveChanges(); userManager.AddToRole(user.Id, "Administrator"); context.SaveChanges(); VideoGameLogicLayer.UserManager myUserManager = new VideoGameLogicLayer.UserManager(); myUserManager.RegisterUser(new User() { Roles = { "Administrator" }, Username = "******" }); } } }
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) { await SignInManager.SignInAsync(user, isPersistent : false, rememberBrowser : false); VideoGameLogicLayer.UserManager myUserManaager = new VideoGameLogicLayer.UserManager(); myUserManaager.RegisterUser(new User() { Username = model.Email }); // For more information on how to enable account confirmation and password reset please visit http://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>"); //Now save the user to my database. return(RedirectToAction("Index", "Home")); } AddErrors(result); } // If we got this far, something failed, redisplay form return(View(model)); }