/// <summary> /// Creates the user to the database /// </summary> /// <param name="viewModel">The form of the model that was used</param> /// <returns>True if successful save, false if not.</returns> public bool Create(AccountViewModel viewModel) { if (viewModel != null) { ApplyChanges(viewModel); // Verify we can create the user Model.Salt = HashManager.GetSalt(); Model.Password = HashManager.HashPassword(viewModel.Password, Model.Salt); Model.CreatedOn = DateTime.Now; Model.InviteCode = Codes.GenerateInviteCode(); bool usernameExists = services.Account.AccountEmailExists(viewModel.Username); bool emailExists = services.Account.AccountEmailExists(viewModel.Email); bool passwordsMatch = viewModel.Password == viewModel.PasswordVerify; if (!usernameExists && !emailExists && passwordsMatch) { services.Account.AddAccount(Model); return(services.Save()); } else { return(false); } } return(false); }
public void HashManager_HashPassword_HashesPassword() { String pass = "******"; String salt = HashManager.GetSalt(); String hash = HashManager.HashPassword(pass, salt); Assert.IsNotNull(hash); }
public void HashManager_ValidatePassword_Returns_True() { String pass = "******"; String salt = HashManager.GetSalt(); String hash = HashManager.HashPassword(pass, salt); bool isPass = HashManager.ValidatePassword(pass, hash); Assert.AreEqual(true, isPass); }
public async Task <ResultModel> Register(RegisterModel model) { var isUserExist = _dbContext.Users.Any(i => i.Username == model.Username); if (!isUserExist) { var password = HashManager.HashPassword(model.Password); var user = _dbContext.Users.Add(new Entities.User { Username = model.Username, Password = password, FirstName = model.FirstName, LastName = model.LastName }); await _dbContext.SaveChangesAsync(); return(new ResultModel { IsSuccessful = true, Data = user, Message = "User successfully registered!", Code = (int)HttpStatusCode.OK }); } else { return(new ResultModel { IsSuccessful = false, Data = null, Message = "Username is already exist!", Code = (int)HttpStatusCode.BadRequest }); } }
private void SeedUsers(ApplicationDbContext context) { var password = HashManager.HashPassword("Password123"); if (!context.Users.Any(i => i.Username == "*****@*****.**")) { context.Users.Add(new Entities.User { Username = "******", Password = password }); } if (!context.Users.Any(i => i.Username == "*****@*****.**")) { context.Users.Add(new Entities.User { Username = "******", Password = password }); } context.SaveChanges(); }