Exemplo n.º 1
0
        public async Task <IActionResult> Login(LoginViewModel model, string returnUrl = null)
        {
            clsQuery consul = new clsQuery();

            ViewData["ReturnUrl"] = returnUrl;
            if (ModelState.IsValid)
            {
                if (consul._loginSession(model.Email, model.Password))
                {
                    List <Claim> claims = new List <Claim>();
                    claims.Add(new Claim(ClaimTypes.Email, model.Email));
                    claims.Add(new Claim(ClaimTypes.Name, model.Email));
                    claims.Add(new Claim(ClaimTypes.Role, "Admin"));
                    claims.Add(new Claim(ClaimsIdentity.DefaultRoleClaimType, "Admin"));
                    ClaimsPrincipal principal = new ClaimsPrincipal();
                    ClaimsIdentity  iden      = new ClaimsIdentity(claims, CookieAuthenticationDefaults.AuthenticationScheme);
                    principal.AddIdentity(iden);
                    //await HttpContext.Authentication.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme, principal, new Microsoft.AspNetCore.Http.Authentication.AuthenticationProperties
                    //{
                    //    IsPersistent = true,
                    //    AllowRefresh=true
                    //});
                    byte[] SessionUSer = System.Text.Encoding.UTF8.GetBytes(model.Email);

                    ViewBag.IsAuth = true;
                    ViewBag.Role   = "Admin";
                    Sec.secjwt JWT = new Sec.secjwt();
                    string     jwt = JWT.GetToken(model);
                    Response.Cookies.Append("Bearer", jwt, new Microsoft.AspNetCore.Http.CookieOptions {
                        Path = "/", HttpOnly = true
                    });
                    Response.Cookies.Append("pandoraRules", Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes(model.Email)), new Microsoft.AspNetCore.Http.CookieOptions {
                        Path = "/", HttpOnly = true
                    });
                    Response.Cookies.Append("IsAuth", Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes(model.Email)), new Microsoft.AspNetCore.Http.CookieOptions {
                        Path = "/", HttpOnly = true
                    });
                    return(RedirectToAction("Index", "PSAV"));
                }
                else
                {
                    ViewBag.result = "Credenciales inválidas";
                }
            }
            else
            {
                ViewBag.result = "Error al llenar el formulario";
            }
            return(View());
        }
        public async Task <bool> IsAuth(string Bearer, string PandoraRules)
        {
            try
            {
                Sec.secjwt ValidToken = new Sec.secjwt();
                bool       validate   = await ValidToken.ValidateTokens(Bearer, PandoraRules);

                isAuth      = validate;
                UsuarioDraw = System.Text.Encoding.UTF8.GetString(Convert.FromBase64String(PandoraRules));
                if (validate)
                {
                    GetMenu();
                }
                return(validate);
            }
            catch (Exception ex)
            {
                isAuth = false;
                return(false);
            }
        }