public async Task <IActionResult> Create([Bind("ID,ProjectName,TaskName,StartDate,EndDate,ClientName,ClientAdress")] CreateProjectModel createProjectModel) { bool valid = true; if (DateTime.Compare(createProjectModel.StartDate, createProjectModel.EndDate) > 0) { ModelState.AddModelError("", "Start Date cannot be greater than End Date"); valid = false; } if (ModelState.IsValid && valid) { _context.Add(createProjectModel); await _context.SaveChangesAsync(); return(RedirectToAction("Index")); } return(View(createProjectModel)); }
public async Task <User> RegisterUser(User user) { // password validation if (string.IsNullOrWhiteSpace(user.Password) || user.Password.Length < 8) { throw new Exception("The password provided is not valid."); } // username validation Regex userNameRegex = new Regex("[a-zA-Z0-9]+"); if (string.IsNullOrWhiteSpace(user.Username) || !userNameRegex.IsMatch(user.Username)) { throw new Exception("Username is not valid, it should only contain alphanumeric characters."); } // email validation Regex emailRegex = new Regex(@"[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?", RegexOptions.IgnoreCase); if (string.IsNullOrWhiteSpace(user.Email) || !emailRegex.IsMatch(user.Email)) { throw new Exception("Please provide a valid email address."); } if (_timeTrackerContext.Users.Count() > 0) { User sameUser = null; // find if a user exists with the same user name sameUser = await _timeTrackerContext.Users.FirstOrDefaultAsync(x => x.Username == user.Username); if (sameUser != null) { throw new Exception("A user with the same username already exists."); } // find if a user exists with the same email sameUser = await _timeTrackerContext.Users.FirstOrDefaultAsync(x => x.Email == user.Email); if (sameUser != null) { throw new Exception("A user with the same email already exists."); } } // password hash generation user.PasswordKey = GeneratePasswordKey(); // password hash user.Password = HashPassword(user.Password, user.PasswordKey); // save the user var addedUser = await _timeTrackerContext.AddAsync(user); await _timeTrackerContext.SaveChangesAsync(); addedUser.Entity.Password = null; addedUser.Entity.PasswordKey = null; // return the user return(addedUser.Entity); }