Exemplo n.º 1
0
 public ActionResult HandleChangePassword(ChangePasswordModels account)
 {
     if (ModelState.IsValid)
     {
         if (string.IsNullOrWhiteSpace(account.Token))
         {
             ModelState.AddModelError("", "Mã xác thực lấy lại mật khẩu trống.");
             account.OldPassword     = "";
             account.NewPassword     = "";
             account.ConfirmPassword = "";
             return(View("Account.ChangePassword", account));
         }
         var result = AccountService.ChangePassword(account.Token, account.OldPassword,
                                                    account.NewPassword);
         if (result == "invalid")
         {
             ModelState.AddModelError("", "Email đăng ký tài khoản không đúng.");
             account.OldPassword     = "";
             account.NewPassword     = "";
             account.ConfirmPassword = "";
             return(View("Account.ChangePassword", account));
         }
         if (result == "current_wrong")
         {
             ModelState.AddModelError("", "Mật khẩu hiện tại không đúng.");
             account.OldPassword     = "";
             account.NewPassword     = "";
             account.ConfirmPassword = "";
             return(View("Account.ChangePassword", account));
         }
         return(Redirect("/"));
     }
     return(View("Account.ChangePassword", account));
 }
Exemplo n.º 2
0
        public IHttpActionResult ChangePassword(int id, [FromBody] ChangePasswordModels newPasswords)
        {
            string logForm = "Form : " + Startup._jsSerializer.Serialize(newPasswords);
            string logAPI  = "[Post] " + Request.RequestUri.ToString();

            if (!ModelState.IsValid)
            {
                return(BadRequest("Invalid data"));
            }

            using (var ctx = new SFDatabaseEntities())
            {
                var existingSuperAdmin = ctx.SuperAdmin
                                         .Where(s => s.Id == id)
                                         .FirstOrDefault();

                if (existingSuperAdmin == null)
                {
                    return(NotFound());
                }

                if (Crypto.VerifyHashedPassword(existingSuperAdmin.Password, newPasswords.OldPassword))
                {
                    existingSuperAdmin.Password = Crypto.HashPassword(newPasswords.NewPassword);
                    try
                    {
                        ctx.SaveChanges();
                    }
                    catch (Exception ex)
                    {
                        StringBuilder logMessage = LogUtility.BuildExceptionMessage(ex);
                        logMessage.AppendLine(logForm);
                        Startup._sfAppLogger.Error(logAPI + logMessage);
                        return(InternalServerError());
                    }
                }
                else
                {
                    return(Unauthorized());
                }
            }
            return(Ok("Success"));
        }
Exemplo n.º 3
0
        public IHttpActionResult ChangePassword(int id, [FromBody] ChangePasswordModels model)
        {
            JavaScriptSerializer js = new JavaScriptSerializer();
            string logForm          = "Form : " + js.Serialize(model);
            string logAPI           = "[Put] " + Request.RequestUri.ToString();

            if (!ModelState.IsValid || model == null)
            {
                Startup._sfAppLogger.Warn(logAPI + " || Input Parameter not expected || " + logForm);
                return(BadRequest("Invalid data"));
            }

            try
            {
                AccountModels accountModel = new AccountModels();
                accountModel.ChangeEmployeePassword(id, model);
                return(Ok("Success"));
            }
            catch (Exception ex)
            {
                switch (ex.Message)
                {
                case "404":
                    return(NotFound());

                case "401":
                    return(Unauthorized());
                }

                StringBuilder logMessage = LogUtility.BuildExceptionMessage(ex);
                logMessage.AppendLine(logForm);
                Startup._sfAppLogger.Error(logAPI + logMessage);

                return(InternalServerError(ex));
            }
        }