Esempio n. 1
0
        public async Task <IActionResult> Login(LoginView login)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    string hashed = Convert.ToBase64String(KeyDerivation.Pbkdf2(
                                                               password: login.Clave,
                                                               salt: System.Text.Encoding.ASCII.GetBytes(configuration["Salt"]),
                                                               prf: KeyDerivationPrf.HMACSHA1,
                                                               iterationCount: 1000,
                                                               numBytesRequested: 256 / 8));

                    var e = repositorioUsuarios.ObtenerPorEmail(login.Email);
                    if (e == null /*|| e.Clave != hashed*/)
                    {
                        ModelState.AddModelError("", "El email y/o el password son incorrectos");
                        return(View());
                    }
                    var claims = new List <Claim>
                    {
                        new Claim(ClaimTypes.Name, e.Email),
                        new Claim("FullName", e.Nombre + " " + e.Apellido),
                        new Claim(ClaimTypes.Role, e.Rol),
                    };
                    var claimsIdentity = new ClaimsIdentity(
                        claims, CookieAuthenticationDefaults.AuthenticationScheme);

                    await HttpContext.SignInAsync(
                        CookieAuthenticationDefaults.AuthenticationScheme,
                        new ClaimsPrincipal(claimsIdentity));


                    return(RedirectToAction(nameof(Index), "Home"));
                }
                return(View());
            }
            catch (Exception ex)
            {
                ModelState.AddModelError("", ex.Message);
                return(View());
            }
        }