public void Execute() { using (var dbContext = new ApplicationDbContext()) { if (!dbContext.Roles.Any()) { SeedApplicaionRole(dbContext); } if (!dbContext.Users.Any()) { _applicationUser = SeedApplicationUser(dbContext); } if (!dbContext.QuestionCategories.Any()) { SeedQuestionCategory(dbContext); } if (!dbContext.Institutions.Any()) { SeedInstitution(dbContext); } if (!dbContext.JobPositions.Any()) { SeedJobPosition(dbContext); } dbContext.SaveChanges(); } }
private static ApplicationUser SeedApplicationUser(DbContext dbContext) { const string email = "*****@*****.**"; const string password = "******"; var userManager = new ApplicationUserManager(new UserStore<ApplicationUser>(dbContext)); userManager.UserValidator = new UserValidator<ApplicationUser>(userManager) { AllowOnlyAlphanumericUserNames = false, RequireUniqueEmail = true }; userManager.PasswordValidator = new PasswordValidator { RequiredLength = 6, RequireNonLetterOrDigit = true, RequireDigit = true, RequireLowercase = true, RequireUppercase = true, }; var user = new ApplicationUser { Email = email, UserName = email, FirstName = "John", LastName = "Doe" }; var result = userManager.Create(user, password); if (!result.Succeeded) return null; userManager.AddToRole(user.Id, "Admin"); return user; }
public async Task<ActionResult> Register(Register model) { if (!ModelState.IsValid) return View(model); var user = new ApplicationUser { UserName = model.Email, Email = model.Email, FirstName = model.FirstName, LastName = model.LastName, PhoneNumber = model.PhoneNumber }; var result = await UserManager.CreateAsync(user, model.Password); if (result.Succeeded) { var role = await _roleManager.FindByIdAsync(model.RoleId); await UserManager.AddToRoleAsync(user.Id, role.Name); if (model.Avatar != null && model.Avatar.ContentLength > 0) { var fileName = $"{Guid.NewGuid()}.{Path.GetFileName(model.Avatar.FileName)}"; FileHelper.SaveFile(new UploadConfig { FileBase = model.Avatar, FileName = fileName, FilePath = FilePath.AvatarRelativePath }); var file = new File { Name = fileName, MimeType = model.Avatar.ContentType, Size = model.Avatar.ContentLength, RelativePath = FilePath.AvatarRelativePath + fileName, FileType = FileType.Avatar, ApplicationUserId = user.Id, CreatedBy = User.Identity.GetUserId(), UpdatedBy = User.Identity.GetUserId() }; _fileRepository.Insert(file); _fileRepository.Save(); } // 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>"); // if role = canidate view () return RedirectToAction("Index", "Account"); } AddErrors(result); // If we got this far, something failed, redisplay form return View(model); }