public async Task <IActionResult> Login(Login login) { try { if (!ModelState.IsValid) { return(View()); } else { string userLogin = login.User; string userPass = login.Password; var user = await _context.Users.SingleOrDefaultAsync(u => u.Login == userLogin); if (user != null) { if (userPass == PassGenerator.Decrypt(user.Password)) { HttpContext.Session.SetString("Name", user.Name); HttpContext.Session.SetInt32("UserID", user.ID); HttpContext.Session.SetString("UserName", user.Login); HttpContext.Session.SetString("UserEmail", user.Email); return(RedirectToAction("Index", "Home")); } else { throw new Exception("Senha inválida."); } } else { throw new Exception("Usuário não encontrado."); } } } catch (Exception e) { ViewBag.Error = e.Message; return(View()); } }
public async Task <IActionResult> ChangePassword(ChangePassword _changePassword) { ViewBag.userName = HttpContext.Session.GetString("UserName"); ViewBag.userID = HttpContext.Session.GetInt32("UserID"); try { User usuario = await _context.Users.FindAsync(_changePassword.Id); if (PassGenerator.Decrypt(usuario.Password) != _changePassword.SenhaAntiga) { throw new Exception("Senha antiga inválida!"); } else { if (_changePassword.NovaSenha == PassGenerator.Decrypt(usuario.Password)) { throw new Exception("A nova senha tem que ser diferente da antiga."); } if (_changePassword.NovaSenha != _changePassword.RepeteNovaSenha) { throw new Exception("As senhas devem ser iguais."); } usuario.Password = PassGenerator.Encrypt(_changePassword.NovaSenha); await _context.SaveChangesAsync(); return(RedirectToAction("Index", "Home", new { id = usuario.ID })); } } catch (Exception e) { ViewBag.Error = e.Message; return(View()); } }