Exemplo n.º 1
0
        private ResetPasswordHashEntity Mapentity(SqlDataReader data)
        {
            ResetPasswordHashEntity result = new ResetPasswordHashEntity();

            result.Uniq = data["PublicID"].ToString();
            result.Hash = data["Hash"].ToString();
            //result.Date = DateTime.Parse(data["Date"].ToString());

            return(result);
        }
Exemplo n.º 2
0
        public bool Create(ResetPasswordHashEntity entity)
        {
            using (SqlConnection connection = new SqlConnection(Connection.String))
            {
                SqlCommand command = connection.CreateCommand();
                command.CommandType = CommandType.StoredProcedure;
                command.CommandText = "ResetPasswordHashCreate";

                command.Parameters.Add(new SqlParameter("@PublicID", entity.Uniq));
                command.Parameters.Add(new SqlParameter("@Hash", entity.Hash));
                command.Parameters.Add(new SqlParameter("@Date", entity.Date));

                connection.Open();
                int result = (int)command.ExecuteNonQuery();
                return(result == 1 ? true : false);
            }
        }
        /*public ResponseMessage<bool> CreateLinkAsync(ForgotPasswordRequest model)
         * {
         *  ResponseMessage<bool> response = new ResponseMessage<bool>();
         *  User user = null;
         *
         *  try
         *  {
         *      //first find the user by the given email
         *      user.Email = _userRepository.FindEmail(model.Email);
         *
         *      //if there is no registered user with the given email, we emmit error
         *      if (user == null)
         *      {
         *          throw new Exception($"There are no registered user with the provided {model.Email} e-mail address.");
         *      }
         *
         *      //now create the reset password token (a hash)
         *      string resetPasswordToken = ResetPasswordUrl.Create(user.PublicID);
         *
         *      ResetPasswordHashEntity data = new ResetPasswordHashEntity(user.PublicID, resetPasswordToken);
         *
         *      //insert the token in the database
         *      _resetPasswordHashRepository.Delete(user.PublicID);
         *      response.IsSuccess = _resetPasswordHashRepository.Create(data);
         *
         *      string link = string.Empty;
         *
         *      if (user.Role.ToLower() == UserRole.Agency.ToString().ToLower())
         *      {
         *          link = $@"https://portalnekretnine.com/reset-password/" + $"{resetPasswordToken}/";
         *      }
         *      else
         *      {
         *          link = $@"https://portalnekretnine.com/admin/reset-password/" + $"{resetPasswordToken}/";
         *      }
         *
         *
         *      //the email message text + link
         #if DEBUG
         *     // string message = CreateResetPasswordEmailBody(@"D:\PortalNekretnine\Beckend\portalnekretnine.services\EmailTemplates\resetPassword.html", link);
         #else
         *     // string message = CreateResetPasswordEmailBody(@"/var/www/portalnekretnine/EmailTemplates/resetPassword.html", link);
         #endif
         *
         *      //send the email
         *      //EmailSender.SendEmailAsync(model.Email, "portalnekretnine.com reset password url", message);
         *
         *      response.ResultObject = true;
         *  }
         *  catch (Exception ex)
         *  {
         *      response.IsSuccess = false;
         *      response.ErrorMessage = ex.Message;
         *      response.ResultObject = false;
         *
         *      _resetPasswordHashRepository.Delete(user.PublicID);
         *  }
         *
         *  return response;
         * }*/

        public ResponseMessage <bool> UpdatePassword(ResetPasswordRequest model)
        {
            ResponseMessage <bool> response = new ResponseMessage <bool>();

            try
            {
                //find the record using the resetPasswordToken
                ResetPasswordHashEntity data = _resetPasswordHashRepository.Find(model.ResetPasswordToken);

                //if no record was found using the token
                if (data == null)
                {
                    throw new Exception($"There has not been request for password reset on www.portalnekretnine.com");
                }

                //if record was found using the provided token
                //we check if it is still valid (token lives 5 minutes)
                DateTime now            = DateTime.Now;
                double   elipsedMinutes = now.Subtract(data.Date).TotalMinutes;

                if (elipsedMinutes > 100)
                {
                    //if token is expired we delete the record from the database
                    response.ResultObject = false;
                    _resetPasswordHashRepository.Delete(data.Uniq);
                    throw new Exception($"Reset token has expired!");
                }

                //if token is still active, then we update the database
                User user = _userRepository.FindByUniq(data.Uniq);
                user.Password = PasswordHasher.Create(model.Password, user.Email);
                _userRepository.Update(user);

                //after update delete the token data form DB
                _resetPasswordHashRepository.Delete(data.Uniq);

                response.ResultObject = true;
            }
            catch (Exception ex)
            {
                response.IsSuccess    = false;
                response.ErrorMessage = ex.Message;
            }

            return(response);
        }
Exemplo n.º 4
0
        public ResetPasswordHashEntity Find(string resetPasswordToken)
        {
            ResetPasswordHashEntity result = null;

            using (SqlConnection connection = new SqlConnection(Connection.String))
            {
                SqlCommand command = connection.CreateCommand();
                command.CommandType = CommandType.StoredProcedure;
                command.CommandText = "ResetPasswordHashFind";

                command.Parameters.Add(new SqlParameter("@Hash", resetPasswordToken));

                connection.Open();
                using (SqlDataReader reader = command.ExecuteReader())
                {
                    while (reader.Read())
                    {
                        result = Mapentity(reader);
                    }
                }
            }

            return(result);
        }