public ActionResult ChangePassword(ChangePasswordReqModel model)
 {
     ViewBag.Class = "display-hide";
     try
     {
         if (ModelState.IsValid)
         {
             model.UserId = UserManager.user.UserId;
             var serialized = new JavaScriptSerializer().Serialize(model);
             var client     = new HttpClient();
             var content    = new StringContent(serialized, System.Text.Encoding.UTF8, "application/json");
             client.BaseAddress = new Uri(HttpContext.Request.Url.AbsoluteUri);
             var result = client.PostAsync(BaseURL + "/api/Employee/ChangePassword", content).Result;
             if (result.StatusCode == HttpStatusCode.OK)
             {
                 var contents = result.Content.ReadAsStringAsync().Result;
                 var Response = new JavaScriptSerializer().Deserialize <ResponseModel>(contents);
                 ModelState.Clear();
                 ModelState.AddModelError("CustomError", Response.response);
                 ViewBag.AlertType = "alert-success";
                 ViewBag.Class     = null;
             }
             else if (result.StatusCode == HttpStatusCode.Unauthorized)
             {
                 var contents = result.Content.ReadAsStringAsync().Result;
                 var Response = new JavaScriptSerializer().Deserialize <ResponseModel>(contents);
                 ModelState.AddModelError("CustomError", Response.response);
                 ViewBag.Class     = null;
                 ViewBag.AlertType = "alert-danger";
             }
             else if (result.StatusCode == HttpStatusCode.NotFound)
             {
                 ModelState.AddModelError("CustomError", "Old Password didn't match.");
                 ViewBag.Class     = null;
                 ViewBag.AlertType = "alert-danger";
             }
             else
             {
                 ModelState.AddModelError("CustomError", "Error occurred");
                 ViewBag.Class     = null;
                 ViewBag.AlertType = "alert-danger";
             }
         }
     }
     catch (Exception ex)
     {
         ViewBag.Class = null;
         ModelState.AddModelError("CustomError", ex.Message);
         ViewBag.AlertType = "alert-danger";
     }
     return(View());
 }
Esempio n. 2
0
        public async Task <ServiceResponseResult> ChangePassword(ChangePasswordReqModel model, long?userid)
        {
            Logger.WriteInformation("Requesting changing password.");
            var user = await _context.User.FirstOrDefaultAsync(x => x.Id == userid);

            var pwdHash = HashUtility.CreatePasswordHash(model.Password, _appSettings.Secret);

            user = user.UpdateIsTemporaryPassword(false)
                   .UpdatePasswordHash(pwdHash)
                   .UpdateLastUpdatedOn(DateTime.UtcNow)
                   .UpdateLastUpdatedBy(user.Id);

            _context.User.Update(user);
            await _context.SaveChangesAsync();

            return(new ServiceResponseResult
            {
                StatusCode = System.Net.HttpStatusCode.OK
            });
        }