public async Task <IActionResult> AlterarSenha(AlterarSenhaModel model) { if (model == null) { throw new ArgumentNullException(nameof(model)); } if (!ModelState.IsValid) { return(View(model)); } ModelState.Clear(); try { var data = new AuthDataFromPassPhrase { UserIdentity = model.Login, KeyContent = model.Senha, SystemCode = settings.IdSistema }; await authService.ChangePassword(data, model.NovaSenha, model.ConfirmacaoSenha, settings.KeyCrypto); ShowSuccessMessage("Senha alterada com sucesso"); return(View()); } catch (Exception ex) { ShowErrorMessage(ex); } return(View()); }
//public async Task<IList<Permissao>> ListaPermissoes(Permissao filtro) //{ // return await authRepository.ListaPermissoes(filtro); //} //public async Task<IList<Empresa>> ListaEmpresas(UsuarioEmpresa filtro) //{ // var list = await usuarioRepository.ListarEmpresas(filtro); // return list.Select(r => r.Empresa).ToList(); //} public async Task <bool> ChangePassword(AuthDataFromPassPhrase userData, string newPassword, string confirmPassword, string key) { if (userData.SystemCode == 0) { throw new Exception("Código do sistema não informado"); } if (userData.UserIdentity == null) { throw new ArgumentNullException(nameof(IAuthData.UserIdentity)); } if (userData.KeyContent == null) { throw new ArgumentNullException(nameof(IAuthData.KeyContent)); } var response = new AuthenticationResponse(); response.User = await authRepository.LogIn(userData.UserIdentity.ToString(), userData.SystemCode); if (response.User != null) { var senha = CryptoUtils.Decrypt(response.User.DescSenha, key); response.Logged = string.Equals(senha, userData.KeyContent.ToString()); if (response.Logged) { if (!newPassword.Equals(confirmPassword)) { throw new Exception("Nova senha e confirmação não podem ser diferentes"); } if (senha.Equals(newPassword)) { throw new Exception("Nova senha tem que ser diferente da anterior"); } authRepository.AlterarSenha(response.User.IdUsuario, CryptoUtils.Encrypt(newPassword, key)); } else { throw new Exception("Usuário/Senha inválido!"); } } else { throw new Exception("Usuário não encontrado"); } return(true); }
public async Task <IActionResult> Login(LoginModel model, string returnUrl, [FromServices] TokenConfigurations tokenConfigurations, [FromServices] SigningConfigurations signingConfigurations) { if (!ModelState.IsValid) { return(View(model)); } ModelState.Clear(); try { var data = new AuthDataFromPassPhrase(); data.UserIdentity = model.Login; data.KeyContent = model.Senha; var response = await authService.LogIn(JsonConvert.SerializeObject(data), settings.KeyCrypto); if (response != null && response.Logged) { response.Token = TokenHelper.GenerateJwtToken(response.User.IdUsuario, tokenConfigurations, signingConfigurations); SharedValues.Session = SharedValues.Session ?? HttpContext.Session; SharedValues.UsuarioLogado = response.User; SharedValues.SuccessMessage = string.Empty; SharedValues.ErrorMessage = string.Empty; //Defina pelo menos um conjunto de claims... var claims = new List <Claim> { //Atributos do usuário ... new Claim(ClaimTypes.Name, response.User.Login), new Claim(ClaimTypes.Role, "Admin"), //new Claim("Nome", response.User.Nome), }; var identity = new ClaimsIdentity(claims, CookieAuthenticationDefaults.AuthenticationScheme); var authProperties = new AuthenticationProperties { ExpiresUtc = DateTimeOffset.UtcNow.AddMinutes(2), IsPersistent = true }; //Loga de fato var login = HttpContext.SignInAsync( CookieAuthenticationDefaults.AuthenticationScheme, new ClaimsPrincipal(identity), authProperties ); return(RedirectToLocal(returnUrl)); } else { throw new Exception(string.Join(" - ", response.Errors.Select(r => r.ToString()))); } } catch (Exception ex) { ShowErrorMessage(ex); } return(View()); }