Ejemplo n.º 1
0
        async Task AddCookiesAsync(AutenticarRespuestaVModel response)
        {
            var claims = new List <Claim> {
                new Claim(ClaimTypes.NameIdentifier, response.Email),
                new Claim(ClaimTypes.Email, response.Email),
            };

            var grandmaIdentity = new ClaimsIdentity(claims, CookieAuthenticationDefaults.AuthenticationScheme);

            var userPrincipal = new ClaimsPrincipal(new[] { grandmaIdentity });
            await HttpContext.SignInAsync(userPrincipal);
        }
        public IActionResult Autenticar([FromBody] AutenticarVModel model)
        {
            AutenticarRespuestaVModel response = _userService.Autenticar(model);

            if (response == null)
            {
                return(new ForbidResult());
            }


            return(Ok(new { Data = response.Token }));
        }
Ejemplo n.º 3
0
        public async Task <IActionResult> Login(AutenticarVModel model)
        {
            if (!ModelState.IsValid)
            {
                return(View(model));
            }

            AutenticarRespuestaVModel response = _userService.Autenticar(model);

            if (response == null)
            {
                TempData["LoginError"] = "Intento de inicio de sesión no válido.";
                ModelState.AddModelError("", "Intento de inicio de sesión no válido.");
                return(View(model));
            }

            await AddCookiesAsync(response);

            TempData["JWToken"] = response.Token;

            return(RedirectToAction("Index", "Home"));
        }