public async Task <ActionResult> DoLogin(SystemUserLoginVM systemUserView) { var result = await _systemUserRepository.ValidateUserAndPassword(systemUserView); if (result.IsSuccess) { var claims = new List <Claim> { new Claim(ClaimTypes.Name, result.Value.FullName), new Claim(ClaimTypes.NameIdentifier, result.Value.Id) }; var userIdentity = new ClaimsIdentity(claims, "login"); ClaimsPrincipal principal = new ClaimsPrincipal(userIdentity); await HttpContext.SignInAsync(principal, new AuthenticationProperties { IsPersistent = true, IssuedUtc = DateTime.Now, ExpiresUtc = DateTime.Now.AddMinutes(15), AllowRefresh = true }); return(RedirectToAction("Index", "Dashboard", null)); } else { ModelState.AddModelError(string.Empty, result.Error); return(View("Index", null)); } }
public async Task <Result <SystemUser> > ValidateUserAndPassword(SystemUserLoginVM systemUserView) { Maybe <SystemUser> existingUser = await _applicationDbContext.SystemUser .Where(x => x.UserName.Value == systemUserView.UserName) .FirstOrDefaultAsync(); if (existingUser.HasValue) { if (!existingUser.Value.IsActive) { return(Result.Failure <SystemUser>("El usuario se encuentra inactivo")); } if (existingUser.Value.Password.Value == SecurityHelper.ComputeSha256Hash(systemUserView.Password)) { return(Result.Ok(existingUser.Value)); } return(Result.Failure <SystemUser>("La contraseña es incorrecta")); } else { return(Result.Failure <SystemUser>("No se encontró el usuario")); } }