Exemple #1
0
        public async Task <IActionResult> Login(Cliente cliente)
        {
            if (ModelState.GetFieldValidationState("Email") == ModelValidationState.Valid && ModelState.GetFieldValidationState("Senha") == ModelValidationState.Valid)
            {
                try
                {
                    await Token.CheckCookies(HttpContext);
                }
                catch (Exception)
                {
                }

                Cliente clienteLogado = new Cliente();

                try
                {
                    clienteLogado = await _cliente.SelectClienteByEmail(cliente, Token.GetToken(HttpContext));
                }
                catch (Exception ex)
                {
                    ViewBag.Erro = ex.Message;

                    return(View());
                }

                var claims = new List <Claim>()
                {
                    new Claim(ClaimTypes.Name, clienteLogado.Nome),      //0
                    new Claim("CPF", clienteLogado.Cpf),                 //1
                    new Claim(ClaimTypes.Email, clienteLogado.Email),    //2
                    new Claim("Id", clienteLogado.IdCliente.ToString()), //3
                    new Claim("Senha", clienteLogado.Senha),             //4
                    new Claim(ClaimTypes.Role, "Cliente")
                };

                ClaimsIdentity usuarioIdentidade = new ClaimsIdentity(claims, "CookieAuthentication");

                ClaimsPrincipal principal = new ClaimsPrincipal(usuarioIdentidade);
                await HttpContext.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme, principal, new AuthenticationProperties
                {
                    AllowRefresh = true,
                    ExpiresUtc   = DateTime.Now.ToLocalTime().AddHours(2),
                    IsPersistent = true
                });

                return(RedirectToAction("Browse"));
            }

            return(View());
        }