public int Create([FromBody] User user) { return(us.AddUser(user)); }
public async Task <ActionResult> Register(RegisterFormModel userInput) { if (StateHelper.GetUserFromCookie(Request) != null) { return(RedirectToAction(nameof(Index))); } if (!ModelState.IsValid || !userInput.IsValid()) { return(View()); } try { var ual = new UserAccessLayer(); if (await ual.GetUserByUsername(userInput.Username) != null) { ViewData["Error"] = "A user with that username already exists"; return(View()); } if (userInput.Username.Length < 3) { ViewData["Error"] = "Your username must have at least 3 characters"; return(View()); } if (!Regex.IsMatch(userInput.Password, @"^(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[^a-zA-Z\d]).{10,}$")) { ViewData["Error"] = "Your password must contain at least 1 number, 1 uppercase letter, 1 lowercase letter, 1 special character and must be at least 10 characters long."; return(View()); } if (userInput.Password.Length > 128) { ViewData["Error"] = "Your password cannot be longer than 128 characters"; return(View()); } var token = StateHelper.GenerateUniqueToken(); var hashSalt = Crypto.GenerateRandomString(64); var passwordHash = Crypto.CalculateArgon2Hash(userInput.Password, hashSalt); var user = new UserModel { Username = userInput.Username, HashSalt = hashSalt, PasswordHash = passwordHash, Token = token, TokenDate = DateTime.Now.AddMinutes(StateHelper.ValidTokenDuration), Role = UserRole.User }; if (!await ual.AddUser(user)) { return(View()); } StateHelper.SetUserCookie(user, Response); return(RedirectToAction(nameof(Index))); } catch { ViewData["Error"] = "An unknown error has occured."; return(View()); } }