public Result <UserViewModel> Login(LoginViewModel user) { try { var userResult = Get(u => u.Email.ToUpper() == user.Email.ToUpper()); if (userResult == null) { throw new Exception(EResultMessage.EmailOrPasswordWrong.ToString()); } if (!userResult.IsConfirmed) { throw new Exception(EResultMessage.UserNotConfirmed.ToString()); } if (!Protected.Validate(user.Password, userResult.HashPassword)) { throw new Exception(EResultMessage.EmailOrPasswordWrong.ToString()); } return(ResultHelper.Succeeded(_tokenManager.GenerateUserToken(userResult))); } catch (Exception e) { return(ResultHelper.Failed <UserViewModel>(message: e.Message)); } }
public Result <bool> ChangePassword(int userId, UserPasswordViewModel password) { try { if (password.NewPassword != password.RepeatedNewPassword) { throw new Exception(EResultMessage.InvalidData.ToString()); } var user = Get(u => u.Id == userId); if (user == null) { throw new Exception(EResultMessage.NotFound.ToString()); } if (!Protected.Validate(password.OldPassword, user.HashPassword)) { throw new Exception(EResultMessage.WrongPassword.ToString()); } user.HashPassword = Protected.CreatePasswordHash(password.NewPassword); Update(user); if (!SaveChanges()) { throw new Exception(EResultMessage.DatabaseError.ToString()); } return(ResultHelper.Succeeded(true, message: EResultMessage.RegistrationDone.ToString())); } catch (Exception e) { return(ResultHelper.Failed <bool>(message: e.Message)); } }