Esempio n. 1
0
        public async Task <IActionResult> Login(UserViewModel user)
        {
            var validatorResult = await _validator.ValidateAsync(user, ruleSet : "Login");

            if (validatorResult?.Errors?.Count > 0)
            {
                TempData["error"] = validatorResult.Errors.Select(e => e.ErrorMessage).ToList();
                TempData.Keep("error");

                return(LocalRedirect("/Account/Login"));
            }

            var _user = _mapper.Map <User>(user);

            var claimsPrincipal = await _userApp.AuthenticateAsync(_user);

            if (claimsPrincipal == null)
            {
                TempData["msg"] = "Usuário ou senha incorretos.";

                TempData.Keep("msg");

                return(LocalRedirect("/Account/Login"));
            }
            else
            {
                await HttpContext.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme, claimsPrincipal,
                                              new AuthenticationProperties
                {
                    IsPersistent = true,
                    ExpiresUtc   = DateTime.UtcNow.AddMinutes(30)
                });

                return(LocalRedirect("/"));
            }
        }