public static ActionResult <UserModel> Execute(Guid webSessionId, PostResetUserPasswordType data, string connectionString) { try { using (var connection = new SqlConnection(connectionString)) { // create command object var command = new SqlCommand(); command.Connection = connection; command.Connection.Open(); // authenticate web session if (!WebSessionCheck.Check(webSessionId, connection, command)) { return(new UnauthorizedResult()); } // get user with same username as given value command.CommandText = @$ " SELECT users.* FROM users WHERE users.username = '******' "; var reader = command.ExecuteReader(); // if no rows returned, no user found with given username if (!reader.HasRows) { reader.Close(); return(new BadRequestResult()); } // read returned rows to get user id reader.Read(); var user = new UserModel(reader); reader.Close(); // hash new password var newSalt = UserController.NewSalt(); var newSaltString = UserController.EncodeSalt(newSalt); var newHashedPassword = UserController.ApplyHash( newSalt, data.newPassword ); // insert new password command.CommandText = @$ " INSERT INTO passwords ( user_id , hashed_password , salt ) VALUES ( '{user.id}' , '{newHashedPassword}'
public ActionResult <UserModel> PostResetUserPassword([FromHeader(Name = "X-websession")] Guid webSessionId, [FromBody] PostResetUserPasswordType data) { return(postResetUserPassword.Execute(webSessionId, data, _configuration["ConnectionStrings:DefaultConnection"])); }