public void SeedApplicationUsers() { var applicationUserRepository = new ApplicationUserRepository(); if (applicationUserRepository.GetApplicationUsers().Any()) { return; } applicationUserRepository.Add(new ApplicationUser { Username = "******", Password = "******" }); }
public ValidationResult Insert(ApplicationUser entity) { AppUserValidator validator = new AppUserValidator(); ValidationResult result = validator.Validate(entity); if (result.IsValid) { entity.IsActive = true; entity.IsDeleted = false; entity.CreatedDate = DateTime.Now; repo.Add(entity); repo.Save(); } return(result); }
public async Task <ActionResult> Register(RegisterViewModel register, string ReturnUrl = null) { ApplicationUserRepository userRepo; ApplicationUserClaimRepository userClaimRepo; ApplicationUser user; //ApplicationUserClaim userClaim; //AuthenticationProperties props = null; const string issuer = "Local Authority"; int id = 0; if (ModelState.IsValid) { try { userRepo = new ApplicationUserRepository(settings, logger, dbc); userClaimRepo = new ApplicationUserClaimRepository(settings, logger, dbc); user = userRepo.FindAll().FirstOrDefault(u => u.NormalizedUserName == register.UserName.ToUpper()); if (user == null) { user = new ApplicationUser() { UserName = register.UserName, NormalizedUserName = register.UserName.ToUpper(), Email = register.Email, NormalizedEmail = register.Email.ToUpper(), EmailConfirmed = true, PhoneNumber = String.Empty, PhoneNumberConfirmed = false, TwoFactorEnabled = false, DOB = DateTime.Now, Department = String.Empty, Active = true, ModifiedDt = DateTime.Now, CreateDt = DateTime.Now, Claims = new List <ApplicationUserClaim>() }; // NOTE: This should be wrapped in a Unit of Work // Add User to the database user.PasswordHash = PasswordHash.HashPassword(register.Password); id = (int)userRepo.Add(user); logger.LogInformation($"Created new user account: {register.UserName}"); // Add default User Claims user.Claims.Add(new ApplicationUserClaim() { UserId = id, ClaimType = ClaimTypes.Name, ClaimValue = user.UserName, ClaimIssuer = issuer, Active = true, ModifiedDt = DateTime.Now, CreateDt = DateTime.Now }); user.Claims.Add(new ApplicationUserClaim() { UserId = id, ClaimType = ClaimTypes.NameIdentifier, ClaimValue = id.ToString(), ClaimIssuer = issuer, Active = true, ModifiedDt = DateTime.Now, CreateDt = DateTime.Now }); user.Claims.Add(new ApplicationUserClaim() { UserId = id, ClaimType = ClaimTypes.Role, ClaimValue = "Basic", ClaimIssuer = issuer, Active = true, ModifiedDt = DateTime.Now, CreateDt = DateTime.Now }); user.Claims.Add(new ApplicationUserClaim() { UserId = id, ClaimType = ClaimTypes.DateOfBirth, ClaimValue = user.DOB.ToString("yyyy-MM-dd hh:mm:ss"), ClaimIssuer = issuer, Active = true, ModifiedDt = DateTime.Now, CreateDt = DateTime.Now }); // Add User Claims to the database foreach (ApplicationUserClaim c in user.Claims) { userClaimRepo.Add(c); } logger.LogInformation($"Assigned default claims to new user account: {register.UserName}"); // Sign in the user await identityManager.SignInAsync(user); logger.LogInformation($"Logged in user: {user.UserName} at IP address: {HttpContext.Connection.RemoteIpAddress}"); if (ReturnUrl == null) { return(LocalRedirect("/Home/LoginSuccess")); } else { return(LocalRedirect($"{ReturnUrl}")); } } else { logger.LogError($"User is already registered: {register.UserName}"); return(LocalRedirect("/Account/UserAlreadyRegistered")); } } catch (Exception ex) { logger.LogError($"Exception registering user({register.UserName}): {ex.Message}"); return(RedirectToAction("Error")); } } return(RedirectToAction("Error")); }