public async Task <IActionResult> Register(User usuario) { try { //Just verify if there is any user with the same nickName if (await _context.Memories.CountAsync() > 0) { foreach (var user in await _context.Users.ToListAsync()) { if (user.Login == usuario.Login) { throw new Exception("Não foi possível realizar o cadastro, nome de usuário já utilizado!"); } if (user.Email == usuario.Email) { throw new Exception("Não foi possível realizar o cadastro, email já utilizado por outro usuário!"); } } } usuario.Password = PassGenerator.Encrypt(usuario.Password); if (ModelState.IsValid) { _context.Users.Add(usuario); await _context.SaveChangesAsync(); return(RedirectToAction("Login")); } else { return(View()); } } 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()); } }