Esempio n. 1
0
    public async Task <Result> ChangePasswordAsync(ModelChangePassword changePassword)
    {
        // check password and confirm password
        if (changePassword.NewPassword != changePassword.ConfirmPassword)
        {
            return(new Result
            {
                StatusCode = ResultCodes.DataError,
                Description = "Password does not match"
            });
        }

        var result = await LoginAsync(new ModelLoginCredentials
        {
            Email    = changePassword.Email,
            Password = changePassword.CurrentPassword
        });

        /// authentication passed
        if (result.StatusCode == ResultCodes.Success)
        {
            using (IDbConnection conn = Connection)
            {
                try
                {
                    string sQuery      = @"UPDATE UserCredentials SET 
                    Password = @Password,
                    Salt = @Salt,
                    LastPasswordChange = @LastPasswordChange
                    WHERE Email = @Email;";
                    string currenttime = DateTime.Now.ToString();
                    string salt        = Salt.Create();
                    changePassword.NewPassword = Hash.Create(changePassword.NewPassword, salt);
                    conn.Open();
                    int effectedrows = await conn.ExecuteAsync(sQuery, new { Password = changePassword.NewPassword, Salt = salt, LastPasswordChange = currenttime, Email = changePassword.Email });

                    if (effectedrows > 0)
                    {
                        return(new Result
                        {
                            StatusCode = ResultCodes.Success,
                            Description = "Password has been changed"
                        });
                    }
                    else
                    {
                        return(new Result
                        {
                            StatusCode = ResultCodes.DBError,
                            Description = "Password  changing fail"
                        });
                    }
                }
                catch (Exception ex)
                {
                    _logger.LogDebug(ex.StackTrace);

                    return(new Result
                    {
                        StatusCode = ResultCodes.Error,
                        Description = "Error, Password  changing fail"
                    });
                }
            }
        }
        else
        {
            return(new Result
            {
                StatusCode = ResultCodes.AuthFail,
                Description = "Old password is not correct"
            });
        }
    }
Esempio n. 2
0
 // [Route("dob/{dateOfBirth}")]
 public async Task <ActionResult <Result> > PasswordChange(ModelChangePassword changePassword)
 {
     return(await _credRepo.ChangePasswordAsync(changePassword));
 }