Ejemplo n.º 1
0
        public virtual async Task <IdentityResult> ChangePassword(string password, string newPassword)
        {
            if (password == newPassword)
            {
                return(IdentityResult.Failed(string.Format("La nueva contraseña debe ser diferente a la actual contraseña")));
            }

            var userAutentificado = application.GetCurrentUser();

            if (userAutentificado == null)
            {
                throw new GenericException(string.Format("Intento de actualizar contraseña  del usuario "),
                                           "Seguridad: No se puede actualizar contraseña, el usuario no esta autentificado");
            }

            var user = await UserManager.FindByIdAsync(userAutentificado.Id);

            if (user == null)
            {
                return(IdentityResult.Failed(string.Format("El usuario no se encuentra autentificado")));
            }

            var resultCambio = await UserManager.ChangePasswordAsync(user, password, newPassword);

            if (!resultCambio.Succeeded)
            {
                return(resultCambio);
            }

            try
            {
                //3. Enviar Mensaje..
                var modelBodyEmail = new ChangePasswordDto();
                modelBodyEmail.Nombres = user.NombresCompletos;
                modelBodyEmail.Usuario = user.Cuenta;
                modelBodyEmail.Fecha   = DateTime.Now;

                var body = await TemplateEngine.Process(Constantes.TEMPLATE_SEGURIDAD_ENVIO_CORREO_CAMBIO_CLAVE, modelBodyEmail);

                var msg = new IdentityMessage();
                msg.Destination = user.Correo;
                msg.Subject     = "Cambio de contraseña";
                msg.Body        = body;
                await EmailService.SendAsync(msg);
            }
            catch (Exception ex)
            {
                var result = ManejadorExcepciones.HandleException(ex);
            }

            return(resultCambio);
        }
Ejemplo n.º 2
0
        public async Task <ActionResult> ChangePassword(ChangePasswordViewModel model)
        {
            if (!ModelState.IsValid)
            {
                return(View(model));
            }
            var result = await AspUserManager.ChangePasswordAsync(User.Identity.GetUserId(), model.OldPassword, model.NewPassword);

            if (result.Succeeded)
            {
                var user = await AspUserManager.FindByNameAsync(User.Identity.GetUserId());

                if (user != null)
                {
                    await SignInManager.SignInAsync(user, isPersistent : false, rememberBrowser : false);
                }
                return(RedirectToAction("Index", new { Message = Message.ChagnePasswordSuccess }));
            }
            else
            {
                TempData["Alert"] = SetAlert.Set("Wprowadzone hasło jest niepoprawne!", "Błąd", AlertType.Danger);
                return(View(model));
            }
        }